I have been wondering how to customize the prompt in bash for some time now. I never had enough time and energy to look into that. Until now! So, let’s doo-ooo-ooo it!
It’s actually one of the simplest 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:
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
- How to: Change / Setup bash custom prompt (PS1)
- Bash Shell PS1: 10 Examples to Make Your Linux Prompt like Angelina Jolie
- BASH Shell change the color of my shell prompt under Linux or UNIX
- Bash reference: Controlling the Prompt