Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

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 can't remember where, but I've already seen the bash prompt's current path shortened in an interesting way: every directory contained in the path (excepted the last one) is replaced by its first letter only. For instance: path/to/some/directory would be shortened to p/t/s/directory.

How can I reproduce that behavior ?

share|improve this question
You can't directly do this in PS1. You would need to set PS1 to contain a variable (PS1='${SHORT_PWD}\\$') and update that variable in $PROMPT_COMMAND. – Gilles Mar 8 '13 at 1:30
up vote 4 down vote accepted

After playing with this for a while I got the answer you require:

Add this to your .bashrc file in your home directory, exit the terminal and renter it and you will get you prompt.

PS1='$(eval "sps")$ '
sps() {
   echo "$PWD" | sed -r 's|/(..)[^/]*|/\1|g'

It uses the declared function sps() to evaluate the path every time the variable PS1 which is the prompt, is displayed


/ho/de/De/Ap/Ti$ pwd

Or...if you insist on the one letter

PS1='$(eval "sps")$ '
sps() {
   echo "$PWD" | sed -r 's|/(.)[^/]*|/\1|g'

Which displays:

/h/d/D/A/T$ pwd
share|improve this answer
Thanks Meer. My prompt is actually removing the user@pc part. How can I handle this? – Alex Dec 28 '15 at 9:18

To truncate all directory names except the last one:

PS1='$(eval "sps")$ '                                                                                
sps() {                                                                                              
    python -c "import sys; dirs = sys.argv[1].split('/'); print '/'.join(d[:1] for d in dirs[:-1]) + '/' + dirs[-1]" $PWD
share|improve this answer

The bash prompt is set by the PS1 variable. Yours is probably set in ~/.bashrc. Mine is on line 58. You will see something like this

if [ "$color_prompt" = yes ]; then


Here a google search I did.

PROMPT_COMMAND='CurDir=`pwd|sed -e "s!$HOME!~!"|sed -re "s!([^/])[^/]+/!\1/!g"`'
PS1="[\$CurDir] \$ "
share|improve this answer
You are not answering the question at all. The difficulty isn't where to set the prompt (Anto already knows about .bashrc) but what to set it to. – Gilles Mar 8 '13 at 1:28
Yep, exactly Gilles, thanks. I've been looking everywhere over the web to find a way to do it, but all I could find so far was simple PS1 substitution, which is not what I want to achieve here. – Anto Mar 8 '13 at 1:30
@Gilles Sorry if I try to teach and not just puke out answers. Here ya go buddy. Socrates would be so proud of me. – shakabra Mar 8 '13 at 1:37
... and everybody knows how all the best teachers love to throw some "I have no idea what you want to do, but it looks like stupid crap" to their students. Moreover this is a Q&A, with, you know, pragmatic questions and answers... If you just want to flame or trash-talk people, you can go back to something like 4chan ;) – Anto Mar 8 '13 at 1:45
Sorry I edited my post. – shakabra Mar 8 '13 at 1:48

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.