Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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 is replaced by its first letter only. For instance: path/to/some/directory would be shortened to p/t/s/d.

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
What have you tried? Have you ever heard of $PS1? –  shakabra Mar 8 '13 at 1:41
add comment

2 Answers

up vote 1 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
add comment

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
add comment

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.