Linux – Set the terminal prompt in Ubuntu to show only the working directory name instead of its full path

linuxUbuntu

I have been searching around for a while and have not been able to find an answer to this. Whenever I use the command line in Ubuntu it always lists the full directory back to my home directory, how to I set it to only show the current working directory.

Best Answer

Best guess for default Ubuntu install

Find where your PS1 variable is set and change \w to \W.

You can do an initial check of this method thus:

user@computer:~/full/path/to/directory$ echo $PS1
${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$
user@computer:~/full/path/to/directory$ export PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\]\$ '
user@computer:directory$

It is probably being set in your .bashrc. If not, check /etc/bashrc and override the variable there in your .bashrc. You will of course have to do an exec bash or source your .bashrc for changes made there to take effect.

Other setups

There are different variations on how to do this depending on what shell you are using and how it is set up. For example, you might conceivably have your prompt set up like this:

export PS1='$USER@$(hostname):$PWD$ '

In which case you will want to do:

export PS1='$USER@$(hostname):$(basename $PWD)$ '