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!

System Admin Handbook

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

This book approaches system administration in a practical way and is an invaluable reference for both new administrators and experienced professionals. It details best practices for every facet of system administration, including storage management, network design and administration, email, web hosting, scripting, and much more.

$ Check price
74.9974.99Amazon 4.5 logo(142+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Prism Glasses

Aching back from coding all day? Try Back Problems

Ever feel achy from sitting crunched up on your computer table? Try lying down with these optical glasses that allow you to work on your laptop while lying flat on your back. This is the perfect solution with those with limited mobility or those who wish to prevent neck cramps and back strains.

$ Check price
4.454.45Amazon 4 logo(128+ reviews)

More Back Problems resources