Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have the following in my .bash_profile (from a similar question here:

PROMPT_COMMAND='pwd2=$(sed "s:\([^/]\)[^/]*/:\1/:g" <<<$PWD)'
PS1='\u@\h:$pwd2\$ '

However, if the current directory is within a .dir (such as ~/.vim/bundle/) then the prompt just displays a .:


I would like it instead to retain 1 char for all dirnames unless it has a dot, in which case it would show two, like this:


Even better would be if I also have the home directory represented by a ~ like this:


Any ideas?

share|improve this question
Is it OK just putting the basename of the current working directory in the prompting instead of the whole directory, and use pwd command if you want to get the whole directory? That means use \W instead \w. This solution is simple and work for me. – Edw4rd Nov 16 '12 at 20:09
up vote 1 down vote accepted

This seems to do the trick, adding an optional . to the capture:

PROMPT_COMMAND='pwd2=$(sed "s:\(\.\?[^/]\)[^/]*/:\1/:g" <<<$PWD)'
PS1='\u@\h:$pwd2\$ '

And for the 'even better':

PROMPT_COMMAND='pwd2=$(sed -e "s:$HOME:~:" -e "s:\(\.\?[^/]\)[^/]*/:\1/:g" <<<$PWD)'
PS1='\u@\h:$pwd2\$ '
share|improve this answer
hmmm...neither of these work on my system: the prompt shows the full dirnames in the path ... chris@DeathStar:/Users/chris/db/personal$ – Chris Nov 16 '12 at 20:38
Hmm. Could be an incompatibility between gnu sed and osx sed. Don't have an osx machine to test on. – Dennis Kaarsemaker Nov 16 '12 at 20:40
Thank you! That pointed me in the right direction... installed gnu-sed and all it works! – Chris Nov 16 '12 at 23:36

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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