Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Bash Syntax

Bash prompt strings (PS1 to PS4)

Reference for Bash prompt variables PS0 to PS4 and PROMPT_COMMAND: backslash escapes like \u and \w, color codes with \[ \], select menus and set -x traces.

Bash shows text before it waits for input, and each kind of waiting has its own variable. PS1 is the primary prompt you see before each command, PS2 is the continuation prompt shown when a command is unfinished, PS3 is the question printed by select, and PS4 is the prefix Bash prints in front of each traced command under set -x. Bash 4.4 added PS0, shown after you press Enter but before the command runs.

The strings accept backslash escapes (such as \u for the user name) and are also run through parameter expansion and command substitution each time they are displayed, so a prompt can show live information like the current Git branch.

Terminal
PS1='\u@\h:\w\$ '          # user@host:directory$
PS2='> '
PS3='Pick one: '
PS4='+ ${BASH_SOURCE##*/}:${LINENO}: '

Try it

Bash Expanding a prompt without an interactive shell

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash PS3 for select and PS4 for tracing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
PS1='...'   # before each command
PS2='...'   # continuation line
PS3='...'   # select
PS4='...'   # xtrace (set -x)
PS0='...'   # after Enter, before the command runs (Bash 4.4+)
PROMPT_COMMAND='commands'   # run before PS1 is shown
Escape Meaning
\u Current user name.
\h, \H Host name up to the first dot, or the full host name.
\w, \W Working directory (with ~ for home), or just its last component.
\$ # if the effective UID is 0, otherwise $.
\d, \t, \T, \@, \A Date, time 24-hour with seconds, 12-hour with seconds, 12-hour with am/pm, 24-hour hours and minutes.
\D{format} Time with strftime format, for example \D{%H:%M:%S}.
\n, \r, \\ Newline, carriage return, backslash.
\j, \l Number of jobs; the terminal device base name.
\!, \# History number; command number of this session.
\s, \v, \V Shell name; version; version with patch level.
\e, \a Escape character (for colors); bell.
\[ \] Start and end of non-printing characters, so line editing counts the width correctly.
\nnn, \xHH A character by octal or hex code.

Returns: prompt variables hold text; they have no status. ${var@P} returns the expanded string.

Errors and edge cases: forgetting \[ \] around color codes makes the cursor position wrong, so long lines wrap over the prompt.

Usage notes

  • Set PS1 in ~/.bashrc, not .profile, because only interactive shells read it. Non-interactive shells have no PS1, so scripts commonly test [ -n "$PS1" ] (or case $- in *i*) to detect an interactive shell.
  • Quote with single quotes. PS1='\w $(git branch --show-current) \$ ' expands the command each time. In double quotes, $(...) would be evaluated once when you set the variable.
  • Colors. Use \[\e[32m\] to turn a color on and \[\e[0m\] to reset. Alternatively \[$(tput setaf 2)\] is more portable across terminals.
  • Slow prompts. Everything in PS1 runs on every prompt. A call to git status in a big repository will make the shell feel sluggish.
  • PROMPT_COMMAND runs just before PS1 is printed; Bash 5.1 also accepts an array of commands. It is the place for setting the window title, saving history (history -a) or recording the exit status of the previous command ($? is available at the start).
  • PS4 trace markers. The first character is repeated for each level of nesting, so a leading + shows depth. Using ${BASH_SOURCE}:${LINENO} makes traces of sourced files readable.
  • PROMPT_DIRTRIM=2 shortens \w to the last two directory components, with ... in front.
  • Non-Bash shells (dash, sh) understand only PS1 and a few escapes, and not \u at all in some of them.