How to Customize (and Colorize) Your Bash Prompt


Most Linux distributions configure the Bash prompt to look something like username@hostname:directory$ . But you can configure the Bash prompt to contain whatever you like, and even choose whatever colors you like.

The example steps here were performed on Ubuntu 16.04 LTS. The process should be the same on other Linux distributions, although the default Bash prompt and settings in the .bashrc file may be a bit different.

Where the Prompt Variable is Stored

Your Bash prompt configuration is stored in your user account’s .bashrc file, which is at ~/.bashrc. So, if your username is bob, the file is at /home/bob/.bashrc.

You can open the file to view the current Bash variable. We’ll use nano as our example text editor, although you could also use vi, emacs, or any other text editor you’re comfortable with. Open a Terminal and run:

nano ~/.bashrc

Scroll down to the PS1= section. The first variable looks rather complicated because it includes color information—we’ll explain that later. The second variable, without color information, reads as follows:

${debian_chroot:+($debian_chroot)}\u@\h:\w\$

This is still a little complicated due to the ${debian_chroot:+($debian_chroot)} bits. These just tell Bash to let you know if you’re using a Debian chroot environment and normally won’t be shown. Ignoring those, here’s the default structure of the Bash prompt variable:

\u@\h:\w\$

\u indicates your username, @ indicates the @ sign, \h indicates the hostname (computer name), : indicates the : character, \w indicates the working directory, and \$ indicates a $ if you’re a normal user account or # if you’re root. So, putting that all together, you get username@hostname:working_directory$.

To change your Bash prompt, you just have to add, remove, or rearrange the special characters in the PS1 variable. But there are many more variables you can use than the default ones.

Leave the text editor for now—in nano, press Ctrl+X to exit. We’ll show you how to experiment with variables before actually writing a new one into your .bashrc file.

How to Create a Custom Bash Prompt

Your Bash prompt configuration is stored in the PS1 variable. To save the contents of the PS1 variable into a new variable, run the following command:

DEFAULT=$PS1

You can now set the PS1 variable to different values to experiment. For example, the first line here would set your prompt to a basic “user$” prompt, while the second would set your prompt to a basic “user:working_directory$” prompt.

PS1="\u\$ " PS1="\u:\w\$ "

If you ever want to get back to your default prompt, just run the following command.

PS1=$DEFAULT

Bash will be restored to its default prompt thanks to the fact that you saved those default settings earlier. Note that any changes you make here are only temporary for the current Bash session, so you can always sign out and sign back in or close and reopen the terminal window to go back to your default prompt. But the above line makes it possible to easily get back to your default Bash prompt without the hassle of signing out or closing a window.

You can add any characters or text to the variable. So, to prefix the default prompt with “Hello World”, you could use:

PS1="Hello World \u@\h:\w\$ "

Now that you’ve got the basics down, you just need to know what all the special characters are. You probably won’t care about many of these, but here’s the full list as it appears in the Bash manual:

  • A bell character: \a
  • The date, in “Weekday Month Date” format (e.g., “Tue May 26”): \d
  • The format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required: \D{format}
  • An escape character: \e
  • The hostname, up to the first ‘.’: \h
  • The hostname: \H
  • The number of jobs currently managed by the shell: \j
  • The basename of the shell’s terminal device name: \l
  • A newline: \n
  • A carriage return: \r
  • The name of the shell, the basename of $0 (the portion following the final slash): \s
  • The time, in 24-hour HH:MM:SS format: \t
  • The time, in 12-hour HH:MM:SS format: \T
  • The time, in 12-hour am/pm format: \@
  • The time, in 24-hour HH:MM format: \A
  • The username of the current user: \u
  • The version of Bash (e.g., 2.00): \v
  • The release of Bash, version + patchlevel (e.g., 2.00.0): \V
  • The current working directory, with $HOME abbreviated with a tilde (uses the $PROMPT_DIRTRIM variable): \w
  • The basename of $PWD, with $HOME abbreviated with a tilde: \W
  • The history number of this command: \!
  • The command number of this command: \#
  • If the effective uid is 0, #, otherwise $: \$
  • The character whose ASCII code is the octal value nnn: \nnn
Sasha Harriet

Sasha Harriet

As content editor, I get to do what I love everyday. Tweet, share and promote the best content our tools find on a daily basis.

I have a crazy passion for #music, #celebrity #news & #fashion! I'm always out and about on Twitter.
Sasha Harriet

Latest posts by Sasha Harriet (see all)


More from Around the Web

Subscribe To Our Newsletter

Join our mailing list to receive the latest news from our network of site partners.

You have Successfully Subscribed!

Pin It on Pinterest