Customizing Your Prompt in BASH
You can customize your bash shell prompt by setting the environment
variable PS1
in your .bashrc
file. You set
PS1
to a string for your prompt. This string can incldue
control sequences, which begin with a \
.
If you do this
PS1="Pay attention, fool!"
your shell prompt will be "Pay attention, fool!". Note that there are no spaces around the = sign. If you have spaces there, you will get an error statement from bash.
Using Control Sequences
For example, the \u
tells bash to insert the name of the
current user into the prompt string. If your user name is
morrison
, your prompt string will be "morrison—>
".
If you are a csh
or tcsh
user and, like many such
people, are used to having a history number in your prompt string, bash can do
this similarly to the C shell: if the sequence \!
is used in the
prompt string, it will substitute the history number. Thus, if you define your
prompt string to be
PS1="\u[\!]> "
your prompt will be look like morrison[1]>
, then
morrison[2]>
, and so on as you keep issuing commands.
If you type
!number
into bash, it will execute the command with history number
number
.
A very useful way to set up your prompt string is so that it always contains your current working directory. This way, you needn't type pwd to remember where you are. Here's how.
PS1="\w--> "
This table provides a list of control sequences that are available.
Prompt String Control Sequences | |
---|---|
Code | Feature |
\a | The ASCII bell character (007) |
\A | The current time in 24-hour HH:MM format |
\d | The date in "Weekday Month Day" format |
\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 |
\e | The ASCII escape character (033) |
\H | The hostname |
\h | The hostname up to the first "." |
\j | The number of jobs currently managed by the shell |
\l | The basename of the shell's terminal device name |
\n | A carriage return and line feed |
\r | A carriage return |
\s | The name of the shell |
\T | The current time in 12-hour HH:MM:SS format |
\t | The current time in HH:MM:SS format |
\@ | The current time in 12-hour a.m./p.m. format |
\u | The username of the current user |
\v | The version of bash (e.g., 2.00) |
\V | The release of bash; the version and patchlevel (e.g., 2.00.0) |
\w | The current working directory |
\W | The basename of the current working directory |
\# | The command number of the current command |
\! | The history number of the current command |
\$ | If the effective UID is 0, print a #, otherwise print a $ |
\nnn | Character code in octal |
\\ | Print a backslash |
\[ | Begin a sequence of non-printing characters, such as terminal control sequences |
\] | End a sequence of non-printing characters |
PS2
is called the secondary prompt string Its
default value is >. It is used when you type an incomplete line and hit
RETURN, as an indication that you must finish your command. You can customize
it in the same manner as PS1
.