Home > Articles

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Customizing Your Prompt

The default Bash interactive prompt is a dollar sign ($), although some distributions use a different symbol. Most Linux distributions redefine the prompt to include additional information, such as your current login and computer, which is useful when you're moving between accounts and computers.

If a variable named PS1 (prompt string 1) is defined, Bash will use the value of this variable for your main prompt. If you include variable names in the string, Bash will substitute the value of the variables into your prompt.

$ declare -x PS1="Bash $ "
Bash $ pwd
/home/kburtch/archive
Bash $

The following declares a three-line prompt with a blank line, the current directory, the old current directory, login name, computer name, and a bold $.

$ declare -x PS1="
\$PWD (\$OLDPWD)
\$LOGNAME@'uname -n'\['tput bold'\] \$ \['tput rmso'\]"

/home/kburtch/archive (/home/kburtch/work)
kburtch@linux_box $

The \[ and \] should surround the display formatting characters returned by tput. Otherwise, Bash assumes that all the prompt characters are printable and will not wrap long input lines properly.

Bash has a PS2 (prompt string 2) variable, which is the prompt for incomplete command lines such as when you use a multiline quotation. By default, this prompt is a greater-than sign (>).

Bash recognizes the following escape sequences in a prompt.

  • \a—A beep (the ASCII bell character)

  • \A—24 time in HH:MM format

  • \d—The date in "weekday-month-date" format

  • \D{s}—Runs the C statftime function with format string s

  • \e—The ASCII escape character

  • \h—The hostname

  • \H—The complete hostname, including the domain

  • \j—The number of jobs in the job table

  • \l—The tty device

  • \n—A new line

  • \r—A carriage return

  • \s—The name of the shell

  • \t—The 24 hour time

  • \T—The 12 hour time

  • \@—The time in AM/PM format

  • \u—The username

  • \v—The Bash version

  • \V—The Bash release

  • \w—The current working directory

  • \W—The basename of the current working directory

  • \!—The position of the command in the history list

  • \#—The sequential command number for the session

  • \$—Default prompt (# for the superuser, otherwise $)

  • \nnn—ASCII character in octal format

  • \\—A backslash

  • \[—Begins a sequence of nonprintable characters

  • \]—Ends a sequence of nonprintable characters

Variables are discussed more in Chapter 5. You can disable variable substitution by turning off the promptvars shell option.

  • + Share This
  • 🔖 Save To Your Account