05. Customizing our Command Prompt $PS1

Ever notice the $ in front of every command in this tutorial series? Surely, the text that appears before your command input is different. Here, we'll show you how to customize this by changing the $PS1 variable.

The $PS1 Variable

The variable that stores the configuration is $PS1 (short for Prompt Statement). To check your own $PS1, use the echo command.

$ echo $PS1

On my personal computer, I use this setting. It outputs the current working directory, then a $ symbol, all in blue text. It then sets the user's font color to default.

Characters used for customization

Here are a list of characters that act as variables that you can use for your prompt statement. They are all preceded by the escape key (\).

ASCII bell character (beep).
Current date eg. Wed May 25.
ASCII escape character.
Hostname of local machine, minus the domain name.
Full hostname.
Total number of jobs managed by the shell session.
The name of current terminal device.
Newline character.
Carriage return.
Name of shell program
Current time in 24-hour HH:MM:SS.
Current time in 12-hour HH:MM:SS.
Current time in 12 hour am/pm format.
Current time in 24 hour HH:MM format.
Username of current user.
Version number of shell.
Version and release numbers of shell.
Current working directory.
Last part of current working directory name.
History number of current command.
Number of commands in this shell session.
Displays a $ symbol for regular uses, # for root.
Signals start of a series for non-printing characters. They are used to manipulate the editor in colors or something else.
Close series.

Let's try building our own!

Let's say we want current time (\@), the current working folder (\w), followed by a greater than symbol (>). Remember to use single quotes to prevent shell expansion!

$ export 'PS1=\@ \w >'
05:01 PM ~/snipcademy/code >

It looks great! Although it would look even better with some color.

Adding color

To add color, we must make use of the ANSI escape code, \e. This tells the terminal not to interpret the sequence, but to interpret it as a command.

adding color to your custom bash terminal

Here are the values that we may use for text font decoration.

Lighter corresponding color.

Since this is a sequence of non-printing characters, we enclose them in \[ and \] characters.

Here is a list of colors you can choose from:

Light Gray
Dark Gray
Light Green
Light Blue
Light Purple
Light Cyan
$ export PS1='\[\e[0;36m\]\@ \w >'

To specify the color that the user types, we add another color to the end of our PS1.

$ export PS1='\[\e[0;36m\]\@ \w >\[\e[1;37m\]'
06:32 PM ~/snipcademy >

Adding text background color

To add a text background color, use a similar method but with these sequences.

Light Gray
$ export PS1='\[\e[0;36m\]\@ \w >\[\e[1;37m\] \[\e[0;44m\]'
06:32 PM ~/snipcademy > User input text here.

Custom cursor positioning

You may also customize your settings so that you can move the cursor line around.

Move to line l, column c.
Move cursor up n lines.
Move cursor down n lines.
Move forward n characters.
Move cursor backward n character.
Clear screen and move cursor to upper-left corner.
Clear from cursor position to end of current line.
Store current cursor position.
Recall stored cursor position.

Persisting your customized prompt

To save your prompt, place it in your .bashrc file.

Take your Linux skills to the next level!

Linux for Beginners

Take your Linux skills to the next level! Try Linux & UNIX

Linux for Beginners doesn't make any assumptions about your background or knowledge of Linux. You need no prior knowledge to benefit from this book. You will be guided step by step using a logical and systematic approach. As new concepts, commands, or jargon are encountered they are explained in plain language, making it easy for anyone to understand.

$ Check price
24.9924.99Amazon 4.5 logo(101+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Self-Massage Tool

Aching back from coding all day? Try Back Problems

Relieve spasms, tight muscles, trigger points and pressure points with the Body Back Buddy! This trigger point massage is designed to help you self-message any area of your body - especially those that are hard to reach. Keeping your muscles relaxes and out of contraction is importan in helping to reduce pain and prevent muscle injury.

$ Check price
29.9529.95Amazon 4.5 logo(3,443+ reviews)

More Back Problems resources