How to Customize Shell Variable PS1-PS4 on Bash Prompt

You can see the real beauty of Linux when you use shell commands. By default, the shell prompt display hostname and working directory. Using variables you can change the command prompt color, display, date time, user name, etc. You can even run shell scripts inside PS1 variable, where PS stands for the prompt statement. I am going to show some examples of changing the Linux environment using variable PS1.

Related Read: How to Change Bash Shell Prompt Colorful in Linux

Presentation of the different shell variable

  • PS1: environment variable which contains the value of the default prompt. It changes the shell command prompt appearance and environment.
  • PS2: environment variable which contains the value the prompt used for a command continuation interpretation. You see it when you write a long command in many lines.
  • PS3: environment variable which contains the value of the prompt for the select operator inside the shell script.
  • PS4: environment variable which contains the value of the prompt used to show script lines during the execution of a bash script in debug mode.

Note that in this article, we will use echo command to see the value of the different prompt in our console.

1) Change the bash prompt PS1

PS1 is the default prompt we see every time when we log in the console. For the most news Linux systems, the defaults values have \u@\h:\w\$ which show the username, hostname, the current working directory and the user privilege. These are just examples of different values it can take. There are some other values that we will see later. Let’s take a look at the PS1 prompt default value on the terminal

# echo $PS1 
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$

The parts that include debian_chroot indicate that if you are operating in a change root environment, the prompt will be modified to remind you. Look below to have a complete idea for the rest of the prompt:
\a The ASCII bell character (you can also type \007)
\d Date in “Sat Sep 04″ format
\e ASCII escape character (you can also type \033)
\h First part of hostname (such as “mybox”)
\H Full hostname (such as “”)
\j The number of processes you’ve suspended in this shell by hitting ^Z
\l The name of the shell’s terminal device (such as “ttyp4″)
\n Newline
\r Carriage return
\s The name of the shell executable (such as “bash”)
\t Time in 24-hour format (such as “23:59:59″)
\T Time in 12-hour format (such as “11:59:59″)
\@ Time in 12-hour format with am/pm
\u Your username
\v Version of bash (such as 2.04)
\V Bash version, including patchlevel
\w Current working directory (such as “/home/koithara”)
\W The “basename” of the current working directory (such as “koithara”)
\! Current command’s position in the history buffer
\# Command number (this will count up at each prompt, as long as you type something)
\$ If you are not root, inserts a “$”; if you are root, you get a “#”
\xxx Inserts an ASCII character based on three-digit number xxx (replace unused digits with zeros, such as “\007″)
\\ A backslash
\[ This sequence should appear before a sequence of characters that don’t move the cursor (like color escape sequences). This allows bash to calculate word wrapping correctly.
\] This sequence should appear after a sequence of non-printing characters.

We will do some examples but before manipulating the value of theses variables, we need to save the file which contains all. It will help to switch back to our original prompt easier in case we make a mistake or without having to log out and back in again.

$ cp ~/.bashrc ~/.bashrc-backup

Example 1: Display only the username and the hostname with the separation character “-”:

user01@server01:~$ export PS1="\u - \h$ "
user01 - server01$ echo $PS1
\u - \h$

We can see immediately the change on our console. Note that after the equals sign, we use the double quote ("") to use the \u and \h values. If we use another annotation, the shell will consider all after the equal sign as the value and we will not have the result we attend.

Example 2: We simulate an error while displaying username and hostname because we will use ^ instead of "

user01@server01:~$ export PS1=^\u - \h$ ^
^\u - \h$ ^$ echo $PS1 
^\u - \h$ ^

Example 3: Passing string to PS1 variable

user01@sever01:~$ export PS1=happy-test$

Example 4: Add time to the prompt with \t and the working directory with \w

user01 - server01$ export PS1="[\t] \u@\h:\w\$ "
[21:38:51] user01@server01:~$

Example 5: Divide the prompt into multiple lines with \n for long command

user01@server01:~$ export PS1="[\t]\n\u@\h:\w\$ "

2) Change the bash prompt PS2

When we are in the console, we can need to associate many commands in one command. It makes the command too long for one line, so it can be broken down into multiple lines by giving “\” at the end of each line. The default interactive PS2 value prompt for a multi-line command is “>“ which indicates that you can continue the command on the second line and so on.

$ echo $PS2

Example 1: The default usage of PS2

# apt-get update && \
> apt-get -y install mysql-client python-setuptools curl git unzip apache2 php && \
> apt-get upgrade

Example 2: You will replace the value by a sentence. Note the escape before the last quote

# export PS2=”incomplete? continue here-> ”
# apt-get update && \
incomplete? continue here-> apt-get -y install curl git unzip apache2 && \
incomplete? continue here-> apt-get upgrade

3) Change the bash prompt PS3

PS3 is used by the select operator inside a bash script. It is difficult to have its value on a simple console. To show what we are talking about, we need to write a simple bash script which will help us to see the value. The default PS3 value prompt is “#?

Example 1: We will copy the content below on a file named

echo "please select a value to display a month on the list below"
select i in jan feb mar apr exit
    case $i in
       jan) echo "January";;
       feb) echo "February";;
       mar) echo "March";;
       exit) exit;;

Example 2: A test of the script to display the value of PS3

$ chmod +x
1) jan
2) feb
3) mar
4) exit
#? 2

Note that “#?” is the PS3 value

Example 3: Modify the default value to “choice” and display the it by executing the script

$ export PS3="choice: "
Select a value to display a month on the below
1) jan
2) feb
3) mar
4) exit
choice: 2

See that choice is our new value

4) Change the bash prompt PS4

The PS4 shows each line of a bash script when we are in debug mode before executing lines. It helps to know which line doesn’t give the result attend. We can display the value of PS4 with echo command. But in a bash script, we can see it using bash -x  for the execution. The default PS4 value prompt is “+

Example 1: Create a file with the same content of and modify the default value

$ echo $PS4
$ export PS4="[script line->:] "
$ chmod +x
$ bash -x 
[script line->: ]echo 'Please select a value to display a month on the list below'
Please select a value to display a month on the list below
[script line->: ]select i in jan feb mar apr bye
1) jan
2) feb
3) mar
4) apr
5) bye
#? 3
[script line->: ]case $i in
[script line->: ]echo March
#? 1
[script line->: ]case $i in
[script line->: ]echo January

To make changes permanent

To make PS1 persistent on reboot, you have to add export command with variable to .bash_profile file.

$cat .bash_profile
export PS1="[\d][\t][\u@\h]"

Then load your default .bash_profile file

$ . .bash_profile


Now you know the role of the different bash prompt and how to modify each one. It is important before modification to save the file and you must be able to restore it if a mistake appears.

4 Comments... add one

  1. Wow that was unusual. I just wrote an really long comment but after I clicked submit my comment didn’t show up. Grrrr… well I’m not writing all that over again. Anyways, just wanted to say great blog!


Leave a Comment