Changing the Linux Shell Prompt

How to modify your shell prompt on Linux
2011-06-30
~
2 min read

I have been wondering how to customize the prompt in bash for some time now. However, I never had enough time and energy to look into that. Until now! So, let’s do it!

It’s actually one of the simpler things. The prompt string is saved in an environment variable called $PS1 (prompt string). In order to change it you simply change the variable’s value and it’s done. So, let’s see…

First we want to know the actual value of the variable. We’ll print it out:

echo $PS1

This should give you something around \u@\h:\w\$ . If you compare it to the actual prompt astro@astro-desktop:~/MyBook$ , you can easily guess what the escape sequences stand for.

  • \u - user name
  • \h - host name
  • \w - full path to current working directory

There are a lot more sequences, you can use. You can even color your prompt! For more information on this topic check out the links at the end of this post.

In order to change the prompt execute the following command:

export PS1="[his dudeness \u@\h:\w]\$ "

This will change your prompt string to something like

[his dudeness astro@astro-desktop:~]$ 

Now, we know how to customize the prompt string, but when you close the shell, it resets back to default. How to make the changes permanent? All you need to do is put the command into the .bashrc file in your home directory.

Actually, here are multiple prompt strings. $PS1 is the primary one. There is also a secondary prompt string that is used to indicate continuation of a multi-line command. For example:

astro@astro-desktop:~$ echo "abc\
> def\
> "
abcdef

In this case, the $PS2 is set to > .

Sources: