Special Prompt Strings

\a
The ASCII BEL character, which is octal 007.
\e
An ASCII Escape character, which is octal 033.
\h
The hostname, up to the first period.
\H
The full hostname.
\n
A newline character, which is octal 012.
\r
A carriage return character, which is octal 015.
\t
The current time in 24-hour HH:MM:SS format.
\T
The current time in 12-hour HH:MM:SS format.
\u
The current user’s username.
\w
The current working directory, with $HOME abbreviated with a tilde (~).
\nnn
The character represented by the octal value nnn.
\[
Start a sequence of nonprinting characters, such as for highlighting or changing colour on a terminal.
\]
End a sequence of nonprinting characters.

Example

# configure the Bash startup file ~/.bashrc
# - set prompt string: [time]user@host:path$
# - set prompt colour: blue if last return value is 0; red otherwise

PS1="\`if (( \$? == 0 )); then echo '\[\e[0;34m\][\t]\u@\h:\w\\$'
  else echo '\[\e[0;31m\][\t]\u@\h:\w\$'; fi\` \[\e[m\]"

2022-01-29