Meaning of \[\e]0; in PS1 in .bashrc

7,793

Solution 1

The \e]0; is an escape sequence; \e is replaced with ASCII 27 (ESC), so the terminal receives the 4 characters ESC ] 0 ; tells xterm to set icon and title bar, that ends in BEL (\a).

So the sequence \e]0;STUFFGOESHERE\a will set the title of the terminal to STUFFGOESHERE. In your example it'll set the title to user/host/path.

FWIW, xterm escape sequences are documented at: https://www.x.org/docs/xterm/ctlseqs.pdf

Solution 2

The characters \e]0; in the line

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

are interpreted by the shell (equating \e to \033 the ASCII ESC escape character), beginning an escape sequence. The sequence ends on \a (likewise interpreted by the shell \007` ASCII BEL bell).

The 0 is a parameter (for what's known as an operating system command), which tells the terminal to change both icon and window titles.

Technically it ought to be \e\\ (ECMA-48), however that's not how it started. When the feature was first introduced in 1986, xterm terminated the title on the first non-printing character. The \a has been recognized as a string terminator by xterm since X11R4 in 1989 (when separate parameters 1 and 2 were added to distinguish the icon and window titles).

rxvt picked this up a couple of years later, and a few years later xterm was modified to also accept a standard string terminator. No particular point was made in the changelog, but it appeared in ctlseqs.ms first in August 1996. Generally when other terminals have implemented the feature they accept \a only.

Without the ending, the isolated escape character could be treated as an error in a terminal-specific manner (including ignoring the text altogether).

Further reading:

Edit: fixed typo

Share:
7,793

Related videos on Youtube

ddzzbbwwmm
Author by

ddzzbbwwmm

Updated on September 18, 2022

Comments

  • ddzzbbwwmm
    ddzzbbwwmm almost 2 years

    In .bashrc

    case "$TERM" in
    xterm*|rxvt*)
        PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
        ;;
    *)
        ;;
    esac
    

    I understand ${debian_chroot:+($debian_chroot)}\u@\h: \w, but not \[\e]0;. What does it make?

  • David Refoua
    David Refoua almost 7 years
    TLDR; That sequence sets the title bar to the working directory.