PS1 is a template, not a picture
The value of PS1 is stored as plain text. It is not expanded when you assign it. Bash expands the backslash escapes in it when it displays the prompt, so the same value can show a different directory each time you change into one. You can see the raw template by echoing the variable, and you can see the expanded result with the @P transformation, which applies prompt expansion to any string.
PS1='\u in \w \$ '
printf '[%s]\n' "${PS1@P}"The second command prints the expanded prompt inside square brackets so that trailing spaces are visible. The \u escape becomes your user name, \w becomes the current directory with your home directory shown as a tilde, and \$ becomes a dollar sign for a normal user or a hash sign for root.
The escapes you will use most
| Escape | Expands to |
|---|---|
\u | The user name |
\h | The host name up to the first dot |
\w | The full working directory, with $HOME shown as ~ |
\W | Only the last part of the working directory |
\$ | # for root, otherwise $ |
\n | A newline, which lets the prompt take two lines |
\j | The number of jobs the shell is managing |
A prompt with the user name, host name, and directory is a common choice. Two more variables complete the picture. PS2 is the continuation prompt that appears when a command spans several lines, and PS4 is the prefix that set -x prints before each traced command.
Colors and non-printing characters
Colors are written as ANSI escape sequences, such as \e[32m for green and \e[0m to reset. These sequences take no space on the screen, but bash cannot know that unless you tell it. Wrap every non-printing sequence in \[ and \]. Without those markers, bash counts the color codes as visible characters, and the cursor position drifts when you edit a long command with the arrow keys.
PS1='\[\e[32m\]\u\[\e[0m\]:\w\$ 'A prompt with color codes that are not enclosed in \[ and \] works at first and then breaks line editing. If the cursor jumps when you use the arrow keys, check the markers before anything else.
Setting the prompt in a startup file
The prompt belongs in ~/.bashrc, because it matters only for interactive shells. Assign the string once, and keep it short. A prompt that takes more than one line for a path is pleasant in a wide window and tiring in a narrow one. Many people keep a second line only when they need one, for example to show the exit status of the last command.
Keeping the prompt short and useful
Bash also has a hook for work that must happen before each prompt. The PROMPT_COMMAND variable holds a command that runs every time bash is about to draw the prompt. It is the natural place to record the exit status of the last command or to refresh a status string, and it keeps that logic out of the escape string itself. A function assigned to PROMPT_COMMAND should be fast and should never print to standard output.
Keep prompt changes small and reversible. Make one change, open a new shell, and check that the prompt still lines up when you edit a long command. A prompt that looks good in a screenshot can still misbehave at the line edge, so a test with a long command is a useful final check.
A prompt is drawn every time you press Enter, so its cost and its clutter matter. A prompt that shows the user, host, and directory is useful when you work across several machines, and one that also shows the exit status of the last command helps when you are debugging. Anything slow, such as a version control status, should be computed by a function that runs before each prompt, not by a command that every escape must wait for. A slow prompt makes the whole shell feel sluggish, and that feeling is hard to trace back to one line.
The host escape has two forms. The lowercase form stops at the first dot, which keeps a long machine name short. The uppercase form shows the full name. A prompt meant for screenshots or shared sessions is better with the short form, because the full name can reveal internal domains.
The working directory escapes also differ. The full form can grow very long in a deep tree, and the last-part form loses the context that tells you where you are. Some people keep both and switch by hand with a small function that reassigns PS1.
A change to PS1 applies only to the shell that reads the file. A terminal that is already open keeps its old prompt until you source ~/.bashrc again or start a new shell. This is worth remembering when a change appears to do nothing.
A two-line prompt is a good example of a template with a purpose. The first line holds the context, such as the user, host, and directory, and the second line holds only the input marker. The input marker stays at the same column every time, so your eye always finds the place to type. Build it with a newline escape in the middle of PS1, and keep the second line short so that a long command has room to wrap.
Reading someone else's prompt
When you see a long prompt string in a dotfile, read it in pieces. First find the escapes and write down what each one expands to. Then find the color markers and check that each start is matched by an end inside the brackets. Finally, print the expanded result with ${PS1@P} to see the actual text, which is often easier than reading the escapes alone.