Ubuntu – find a complete reference for the $PS1 variable


The default PS1 variable on my machine (Kubuntu 13.10) is this:

\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$

I'm looking for a reference for how the $PS1 variable works that, at a minimum, allows me to understand the above PS1 variable.

Best Answer


So far, there hasn't been a single reference for everything that can go in a Bash prompt - but as it's a feature that has evolved over decades and probably varies from distro to distro, maybe that's too much to ask. I've tried to summarise what I found most useful here.

This how-to is the most complete, but is very long and rambling. Some of the more useful sections:

  • Section 2.4 and 2.5 explain the basics of setting PS1, including the (printable) escape characters.
  • Section 3.4 explains why \[ and \] are necessary.
  • Section 6 explains all the main (non-printable) escape sequences you might want to use, including setting the colour of the prompt and the title of an xterm window.

This guide explains how ${} works in Bash in general, and this Ask Ubuntu question explains a bit more about how that works with debian_chroot.

Between those, I think every character in the default Ubuntu PS1 variable is explained.

Explanation of the Ubuntu prompt

There are three parts to the prompt:

  • \[\e]0;\u@\h: \w\a\] sets the title bar of an xterm window:

    • \[ starts a section of non-printable characters
    • \e]0; is the escape sequence for 'set xterm title' (I believe numbers other than 0 will set other xterm properties, though I haven't tested this)
    • \u@\h: \w the title to use (see below for \u, \h and \w)
    • \a marks the end of the title
    • \] marks the end of non-printable characters
  • ${debian_chroot:+($debian_chroot)} expands to the value of $debian_chroot in parentheses if $debian_chroot is set. See this question for more information about $debian_chroot.

  • \u@\h:\w\$ is the prompt itself:

    • \u expands to the current username
    • \h expands to the current hostname
    • \w expands to the current working directory
    • \$ expands to # for root and $ for all other users
Related Question