How to change the default shell prompt.

Solution Verified - Updated -

Environment

  • Red Hat Enterprise Linux

Issue

  • How to change the default shell prompt.
  • How to change the shell prompt so that it will help to identify production systems.

Resolution

  • The shell prompt is controlled via the PS environment variables.
**PS1** - The value of this parameter is expanded and used as the primary prompt string. The default value is \u@\h \W\\$ .
**PS2** - The value of this parameter is expanded as with PS1 and used as the secondary prompt string. The default is ]
**PS3** - The value of this parameter is used as the prompt for the select command
**PS4** - The value of this parameter is expanded as with PS1 and the value is printed before each command bash displays during an execution trace. The first character of PS4 is replicated multiple times, as necessary, to indicate multiple levels of indirection. The default is +
  • PS1 is a primary prompt variable which holds \u@\h \W\\$ special bash characters. This is the default structure of the bash prompt and is displayed every time a user logs in using a terminal. These default values are set in the /etc/bashrc file.

  • The special characters in the default prompt are as follows:

\u = username
\h = hostname
\W = current working directory
  • This command will show the current value.
# echo $PS1
  • This can be modified by changing the PS1 variable:
# PS1='[[prod]\u@\h \W]\$'   
  • The modified shell prompt will look like:
[[prod]root@hostname ~]#
  • In order to make these settings permanent, edit the /etc/bashrc file:

Find this line:

[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "

And change it as needed:

[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[[prod]\u@\h \W]\\$ "

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments