When managing a linux/unix operating system from the command line, users are interacting with the system via shell. This article will explore some of the basic features of the bash shell prompt. Default bash command line prompt on many linux systems does not have a color and display information about user's username, hostname and current working directory. As you well see in the following sections of this document this default settings can be easily changed by exporting a bash prompt PS{n} variables. Bash prompt can be modified to suit users needs and can display time, load, number of users using the system, uptime and more.

Bash prompt variables

As anything else in the linux system also bash prompt can be customized. Bash prompt can be customized by changing the values of bash PS1, PS2, PS3, PS4 variables. To keep the things simple, this article will be concerned just with PS1 and PS2 variables. Use echo command to see the current value of PS1 and PS2 variables.

echo "Bash PS1 variable:"  $PS1
echo "Bash PS2 variable:" $PS2

Bash prompt variables

Subscribe to Linux Career NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.

Bash PS1 prompt variable

PS1 is a primary prompt variable. Currently it holds \u@\h:\w\$ special bash characters. This is the default structure of the bash prompt on many Linux systems and is displayed every time you log in using a terminal.Please see the following section "Bash prompt special characters" for explanation of \u, \h, \w and \$ symbols. Here is a classical bash prompt with default settings:

Bash PS1 prompt variable

Bash PS2 prompt variable

PS2 bash shell variable is a secondary prompt. This prompt is displayed for if the shell waits for a user input, for example your forget to insert second quotation.

Bash PS2 prompt variable

Linux Bash prompt

Bash prompt special characters

Bash prompt can be customized by use of special characters. This table contains bash prompt special characters and its explanation.

Bash special characterBash special character explanationBash special characterBash special character explanation
\a an ASCII bell character (07) \d the date in "Weekday Month Date" format (e.g., "Tue May 26")
\] end a sequence of non-printing characters \e an ASCII escape character (033)
\h the hostname up to the first `.' \H the hostname
\j the number of jobs currently managed by the shell \l the basename of the shell's terminal device name
\n newline \r carriage return
\s the name of the shell, the basename of $0 (the portion following the final slash) \t the current time in 24-hour HH:MM:SS format
\T the current time in 12-hour HH:MM:SS format \@ the current time in 12-hour am/pm format
\A the current time in 24-hour HH:MM format \u the username of the current user
\v the version of bash (e.g., 2.00) \V the release of bash, version + patchelvel (e.g., 2.00.0)
\w the current working directory \W the basename of the current working directory
\! the history number of this command \# the command number of this command
\$ if the effective UID is 0, a #, otherwise a $ \nnn the character corresponding to the octal number nnn
\\ a backslash \[ begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
\D{format} 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

Bash prompt customization

After user login to the systems user environment variables initialized from various files like:

  • global system files /etc/profile or /etc/bashrc
  • user files ~/.bash_profile , ~/.bash_login , ~/.profile , ~/.bashrc or ~/.bash_logout

It is important to know that all users environment variable have a life time equal to the terminal session. When the terminal session is closed the user's variables including bash shell variables defined during a terminal session are emptied and a again redefined when new terminal session is created either via logo in shell or interactive shell. Lets define two variables to prove this statement.

Permanent bash variable definition

First permanent variable we define in one of the bash initialization files ~/.bash_profile and second variable we define on the shell prompt. Let's define permanent user variable:

Define Permanent bash user variable

What happened here, is that user "prompt" modified its own .bash_profile initialization file located in his/her home directory by appending a VAR1 variable definition. When user "prompt" logged out and logged in again the $var1 variable is initialized and available for the new terminal session.
On the same principles we can define our bash prompt. The best place to do it is that bash initialization file .~/bashrc. open up your ~/.bashrc file and add/edit the line defining a PS1 variable to something like:


NOTE: Your ~/.barshrc file may differ from the example below !

define new bash prompt
After the user "prompt" restarts a terminal session he/she is welcomed by new bash prompt. This changes to the bash prompt are permanent.
HINT: Use "source" command to re-read a bash initialization .bashrc file instead of restarting a terminal session.

source .bashrc 

or similarly

. .bashrc 

bash prompt definition

Temporary bash variable definition

Temporary bash variable definition is defined only for a lifetime of the current terminal session. This is tome by an export command. bash temporary variable
As you can see the variable $VAR2 is not defined when user closes his/her terminal session. The permanent variable $VAR1 is always defined from the bash initialization file: ~/.bash_profile . As we can use an export command to define new bash variables we can also use it to modify a bash prompt $PS1 variable. To change a current bash prompt to display only time we could do:

export PS1="\t: " 

define bash temporary prompt

Changing foreground and background bash prompt colors

Syntax for changing colors in the bash is as follows:

3[ -> Indicates the beginning of color in the text
x;yzm - Indicates color code
3[00m - Indicates the end of color in the text

Bash color codes:

Bash color codes

export PS1="3[01;31mBASH IN RED3[00m: " 

bash prompt and red color

Bash Prompt Examples

To get you started with your new bash prompt here are couple examples:

Display current Time

export PS1="\u@\h \t:$ " 

Display current Time with bash prompt

Counting Files in the Current Directory

This bash prompt displays current number of files and directories in the current directory.

export PS1="\u@\h [$(ls | wc -l)]:$ " 

Counting Files in the Current Directory in bash prompt

Submit your RESUME, create a JOB ALERT or subscribe to RSS feed on
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
Get extra help by visiting our LINUX FORUM or simply use comments below.

You may also be interested in: