top of page
book of spells.webp

Start your journey towards mastering the CLI with our FREE Command Line Book of Spells.

By entering your email address you agree to receive emails from Command Line Wizardry. We'll respect your privacy and you can unsubscribe at any time.

  • Writer's picturePaul Troncone

Customizing Your Linux bash Prompt


Displaying the $PS1 variable in Ubuntu
Displaying the $PS1 variable in Ubuntu

When you open the Linux terminal you are greeted with a line of text known as the prompt. The prompt displayed varies by Linux distribution, but it is usually some combination of your username and current working directory. Here is an example:

wizard@ubuntu:~/Desktop$

In the bash shell this prompt is stored in the environment variable $PS1. You can use the echo command to display the contents to the screen:

wizard@ubuntu:~$ echo $PS1

\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$

The string looks pretty complicated, but you can ignore that for now. Just realize that whatever you store in the $PS1 variable will be displayed as your prompt. Below I change the standard Ubuntu prompt to "wizard@CommandLineWizardry$":

wizard@ubuntu:~/Desktop$ export PS1='wizard@CommandLineWizardry$ 

wizard@CommandLineWizardry$ 

Saving your custom prompt in .bashrc


When you update the $PS1 variable using the method above it is only set for your current shell session. As soon as you close your shell the prompt will return to its previous value. That's because the value of $PS1 is typically set automatically by your .bashrc file when the shell is launched. To make your prompt change permanent you need to modify your .bashrc file.


First make a backup of your .bashrc file:

wizard@CommandLineWizardry$ cp ~/.bashrc ~/.bashrc_bak

Next open your .bashrc file using your favorite text editor:

wizard@CommandLineWizardry$ vi ~/.bashrc

Search through the file and identify where the $PS1 variable is being set. In my instance of Ubuntu it looks like this:

.
.
.
if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
.
.
.

Next, update the variable to your desired prompt and save the file.

if [ "$color_prompt" = yes ]; then
    PS1='My New Prompt$ '
else
    PS1='My New Prompt$ '
fi

Finally, save the file and exit your editor. The next time you launch the shell your new prompt will be displayed.

Example of custom prompt saved to .bashrc
Example of custom prompt saved to .bashrc

Advanced Customization


Remember that confusing looking string from earlier when you echoed the $PS1 variable? Much of that was special character strings that allow you to customize the prompt with a variety of information. Here are some of the more popular special characters, which are designated using the backslash character:


Below is an example of using these to customize the prompt to include the time, username, hostname, and current working directory. The string used is '\@ \u@\h[\w]\$ '


wizard@ubuntu:~/Desktop$ PS1='\@ \u@\h[\w]\$ '

11:52 AM wizard@ubuntu[~/Desktop]$

Note - Remember that you would need to update your .bashrc file to make this change permanent.


Adding color and format to the prompt


Using special characters you can also set:

  • Font color

  • Background color

  • Font format

The colors and format are specified using color codes (numbers). Here is a mapping for the primary colors:

For example, if you want white text on a blue background you would use numbers 37 and 44.


For font format you can use:

  • 0 for standard

  • 1 for bold

  • 4 for underlined.

To specify color and format you need to use a specific sequence of characters. There are multiple ways of doing this, but here is my preference:

\[\e[FONTCOLOR;BACKGROUNDCOLOR;FONTFORMATm\]

Note - \e can also be written as \033


For example, to specify white text, blue background, and bold font you would use the following string sequence:

\[\e[37;44;1m\]

Any text that follows this special string sequence would be displayed in that color and format.

PS1='\[\e[37;44;1m\]My Prompt$ '

Adding color to the prompt
Adding color to the prompt

Now lets update the example custom prompt from earlier ('\@ \u@\h[\w]\$ ') with a little color:

PS1='\[\e[32m\]\@ \[\e[34m\]\u@\h\[\e[37m\][\w]\$ '

Lets break this down to make it a little easier to read:


And here is the result:

Adding multiple colors to the prompt
Adding multiple colors to the prompt

Updating the prompt in zsh


You can customize your prompt in zsh shell in a similar fashion, but instead of editing your .bashrc file you need to update your .zshrc file. Also, in the .zshrc file you will update the $PROMPT variable, not the $PS1 variable as in the bash shell. Color formatting in zsh is also done differently, but that is beyond the scope of this article.

2,326 views0 comments
bottom of page