In Bash, why is PROMPT_COMMAND set to something invisible?

5,863

\033 is the octal code for the Esc (Escape) character, which is a good hint that the echoed strings in your PROMPT_COMMAND are terminal control sequences. Both sequences in your examples look like they set the terminal title to user@host:pwd.

The first case, xterm* sets the window name and icon title. For a detailed explanation, look at the list of xterm control sequences and scroll down until you find OSC P s; P t; ST under Operating System Controls (OSC is ESC ] and ST is ESC \).

The second case is for the screen terminal emulator, and in the list of screen control sequences, it explains that ESC _ sets screen's hardstatus (simply put, that's the title of the screen window).

Share:
5,863

Related videos on Youtube

Belmin Fernandez
Author by

Belmin Fernandez

Learning and helping.

Updated on September 18, 2022

Comments

  • Belmin Fernandez
    Belmin Fernandez almost 2 years

    On RHEL6 and CentOS 6, /etc/bashrc sets PROMPT_COMMAND here:

    case $TERM in
    xterm*)
        if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
            PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
        else
            PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\007"'
        fi
        ;;
    screen)
        if [ -e /etc/sysconfig/bash-prompt-screen ]; then
            PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen
        else
            PROMPT_COMMAND='echo -ne "\033_${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\033\\"'
        fi
    

    All of these options, as far as I know, are printed invisibly. What is the use of this?

    I know that PROMPT_COMMAND is to be executed before display the prompt (PS1 usually). I do not understand why echoing something that is not visible is of any use.