This question already has an answer here:

I'm currently working in a really long directory. Probably something like:


It would be awesome if I could execute a command when I'm in this directory and it would only show *sername@hostname ~/test_case$ instead of this wall of text. When I restart bash it should just be normal again. I'm totally fine with executing this command every time I'm in this directory.

How can I achieve something like this?

marked as duplicate by muru, don_crissti, garethTheRed, G-Man, chaos Nov 13 '15 at 23:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Try setting PROMPT_DIRTRIM=1. It's not exactly what you want, but shortens the directory considerable. – chepner Nov 13 '15 at 17:33

(bash 4 or later)

If you set PROMPT_DIRTRIM to a non-zero number, it will replace directories after ~ with ..., retaining the given number of trailing directories. Setting it to 1, for example, will give you

username@hostname ~/.../test_case$
  • Thank you really much! Do I have to put it in my .bashrc. It somehow doesn't seem to work if I just write it in the command-line. – LastSecondsToLive Nov 13 '15 at 17:55
  • I forgot to check before posting; PROMPT_DIRTRIM was introduced in bash 4; you appear to be using an earlier version. – chepner Nov 13 '15 at 17:58
  • That is quite possible. I'm actually on a Mac at the moment. I'll check right away :) Thanks again for the help! – LastSecondsToLive Nov 13 '15 at 18:14
  • Mac OS X still ships with bash 3.2. – chepner Nov 13 '15 at 18:28
  • Yeah, I just saw it as well. I tried to write a small bash script to update my $PS1 to something with \W(like the answer above mentioned). But somehow my PS1 isn't updating.. Do you have any idea why? The script looks like this: #!/bin/bash echo This is a test! export PS1=">" The Test phrase is printed, but the variable doesn't change... – LastSecondsToLive Nov 13 '15 at 18:36

Replace the \w escape sequence in your normal PS1 with \W. Thus, from your example, it would be something like:

PS1='\u@\h \W\$ '
  • That only replace /home/user with ~; it affect the rest of the path. – chepner Nov 13 '15 at 17:34
  • No, \W shows the basename of the current directory, replacing it with ~ only if the CWD is the homedir. Read man bash. – Tom Hunt Nov 13 '15 at 17:35
  • I did, but misread and misremembered. Sorry. – chepner Nov 13 '15 at 17:37
  • This is why I love stackexchange. 5 minutes in and already got a quick solution! – LastSecondsToLive Nov 13 '15 at 17:54
  • I tried to put it into a bash script: #!/bin/bash export PS1=">" but it doesn't seem to change anything. What am I doing wrong? I added the path the file is located in to my PATH in .bashrc_profile and made it executable. I can tell the script is being executed by adding an echo`but somehow PS1 doesn't change. – LastSecondsToLive Nov 13 '15 at 18:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.