I edited the .bashrc file

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u\[\033[00m\]:\[\033[01;36m\]\W:\[\033[01;34m\]\W \[\033[00m\]\$ '

    PS1='[${debian_chroot:+($debian_chroot)}\u:\W \$ ] '

so my prompt looks like this user:Pictures:Pictures $

but I want after my username to show my previous directory.

I know that with the command basename $(dirname 'pwd') I can extract the previous directory.

Is there any way I can use it so my prompt looks like this ?

user:prevDir:Pictures $

thank you very much (and sorry if my English or terminology isn't very good I am new to Ubuntu)


Replace the first \W with ${OLDPWD/\/home\/$USER/\~}:

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u\[\033[00m\]:\[\033[01;36m\]${OLDPWD/\/home\/$USER/\~}:\[\033[01;34m\]\W \[\033[00m\]\$ '
~$ cd tmp
~/tmp$ PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u\[\033[00m\]:\[\033[01;36m\]${OLDPWD/\/home\/user/\~}:\[\033[01;34m\]\W \[\033[00m\]\$ '
user:~:tmp $ 

${OLDPWD/\/home\/$USER/\~} is a parameter expansion which replaces a potential expansion of /home/$USER at the start of $OLDPWD with ~, in order to print ~ instead of the user's home directory's full path (miming the behavior of the default prompt).

If that's undesired, replace \W with just $OLDPWD:

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u\[\033[00m\]:\[\033[01;36m\]$OLDPWD:\[\033[01;34m\]\W \[\033[00m\]\$ '
~$ cd tmp
~/tmp$ PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u\[\033[00m\]:\[\033[01;36m\]$OLDPWD:\[\033[01;34m\]\W \[\033[00m\]\$ '
user:/home/user:tmp $ 
  • thank you man you were very helpfull and that worked perfectly . The only problem I have is that I wanted if it's possible to have only the last directory .(user:user:tmp and not user:/home/user:tmp) . Using this command on terminal basename $(dirname 'pwd')i get what i want but i cannot find a way to use it inside bashrc file .Thank you again for your help. – l.g.karolos Feb 20 '16 at 21:11
  • @user2913267 Do you mean something like this? PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u\[\033[00m\]:\[\033[01;36m\]$(basename "$OLDPWD"):\[\033[01;34m\]\W \[\033[00m\]\$ '. – kos Feb 21 '16 at 3:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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