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
\n\[\033[1;34m\]\W$\[\033[0m\]

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 (\).

\a
ASCII bell character (beep).
\d
Current date eg. Wed May 25.
\e
ASCII escape character.
\h
Hostname of local machine, minus the domain name.
\H
Full hostname.
\j
Total number of jobs managed by the shell session.
\l
The name of current terminal device.
\n
Newline character.
\r
Carriage return.
\s
Name of shell program
\t
Current time in 24-hour HH:MM:SS.
\T
Current time in 12-hour HH:MM:SS.
\@
Current time in 12 hour am/pm format.
\A
Current time in 24 hour HH:MM format.
\u
Username of current user.
\v
Version number of shell.
\V
Version and release numbers of shell.
\w
Current working directory.
\W
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.

0
Default.
1
Lighter corresponding color.
4
Underline.
5
Blinking.
7
Inverse.

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:

[0;30m
Black
[0;31m
Red
[0;32m
Green
[0;33m
Brown
[0;34m
Blue
[0;35m
Purple
[0;36m
Cyan
[0;37m
Light Gray
[1;30m
Dark Gray
[1;31m
Orange
[1;32m
Light Green
[1;33m
Yellow
[1;34m
Light Blue
[1;35m
Light Purple
[1;36m
Light Cyan
[1;37m
White
$ 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.

\e[0;40m
Black
\e[0;41m
Red
\e[0;42m
Green
\e[0;43m
Brown
\e[0;44m
Blue
\e[0;45m
Purple
\e[0;46m
Cyan
\e[0;47m
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.

\e[l;cH
Move to line l, column c.
\e[nA
Move cursor up n lines.
\e[nB
Move cursor down n lines.
\e[nC
Move forward n characters.
\e[nD
Move cursor backward n character.
\e[2J
Clear screen and move cursor to upper-left corner.
\e[K
Clear from cursor position to end of current line.
\e[s
Store current cursor position.
\e[u
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!

Command Line Kung Fu

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

Command Line Kung Fu is packed with dozens of tips and practical real-world examples. You won't find theoretical examples in this book. The examples demonstrate how to solve actual problems. The tactics are easy to find, too. Each chapter covers a specific topic and groups related tips and examples together.

$ Check price
14.9914.99Amazon 4.5 logo(27+ 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

Ad