Customizing the Bash Prompt (PS1)
The prompt is the text an interactive Bash prints when it is ready for the next command, such as ana@laptop:~/projects$. Its content comes from the shell variable PS1 (prompt string 1). Bash reads PS1 again before every prompt, so changing the variable changes the prompt immediately, and putting the assignment in ~/.bashrc makes the change permanent.
A good prompt is a small dashboard. It can show who you are logged in as and on which machine (important when several SSH sessions are open), the current folder, the Git branch, whether the last command failed, and whether background jobs are still running. This lesson covers the special codes Bash understands in PS1, colors and the rule that keeps long prompts from breaking line editing, the related variables PS2 and PROMPT_COMMAND, and a complete two-line prompt.
The Prompt Variables
| Variable | Printed when |
|---|---|
PS1 | the shell is ready for a new command |
PS2 | a command continues on the next line (default > ) |
PS3 | select asks for a choice (lesson 86) |
PS4 | before each line of an xtrace debug trace (default + , lesson 126) |
PS0 also exists: it is printed after you press Enter and before the command runs, and is rarely used. Only interactive shells print PS1 and PS2; scripts never show a prompt.
Prompt Escape Sequences
Inside PS1, Bash replaces special backslash sequences with current information. The @P operator (lesson 152) expands a string exactly as Bash would expand a prompt, which makes it easy to try sequences without changing your real prompt. This test script ran in the folder /home/ana/projects with HOME set to /home/ana:
show() { local PS1=$1; printf '%-22s -> %s\n' "$1" "${PS1@P}"; }
show '\u@\h:\w\$ '
show '\W\$ '
show '\H'
show '\s-\v (\V)'
show '\d \t'
show '\T \@ \A'
show '\D{%Y-%m-%d %H:%M}'
show 'jobs:\j'
show 'shell level ${SHLVL}'
\u@\h:\w\$ -> root@vm:/home/ana/projects#
\W\$ -> projects#
\H -> vm
\s-\v (\V) -> bash-5.2 (5.2.21)
\d \t -> Tue Nov 24 04:00:35
\T \@ \A -> 04:00:35 04:00 AM 04:00
\D{%Y-%m-%d %H:%M} -> 2026-11-24 04:00
jobs:\j -> jobs:0
shell level ${SHLVL} -> shell level 2
| Sequence | Expands to |
|---|---|
\u | the user name |
\h, \H | the host name up to the first dot, and the full host name |
\w | the current folder, with your home folder shortened to ~ |
\W | only the last part of the current folder |
\$ | # for the root user, $ for everyone else |
\d | the date as "Tue Nov 24" |
\t, \T, \@, \A | the time: 24-hour with seconds, 12-hour with seconds, 12-hour with AM/PM, 24-hour HH:MM |
\D{format} | the time in any strftime format, like date +format |
\j | the number of jobs the shell is managing |
\s, \v, \V | the shell name, its version, and its version with patch level |
|