Prompt Escape Characters – How to Use Bell and Escape Characters in Prompt Strings

escape-charactersprompt

My prompt string is printed using this statement,

printf '\033]0;%s@%s:%s\007' user host /home/user

Why does it need an escape character (\033) and a bell character (007)?
When i ran the same command manually, it prints nothing.

When i removed the escape characters and gave the command as,

printf '%s@%s:%s' user host /home/user

it prints,

user@home:/home/user

which is easier to understand.

So, how does the escape characters, \033 and 007 get converted to a shell prompt string?

Best Answer

Only \033 is an escape and it initiates the escape sequence up until and include the ;. \033]0;. This initiates a string that sets the title in the titlebar of the terminal and that string is terminated by the \007 special character.

See man console_codes:

   It accepts ESC ] (OSC) for the setting of certain resources.  In  addi‐
   tion  to  the ECMA-48 string terminator (ST), xterm(1) accepts a BEL to
   terminate an OSC string.  These are a few of the OSC control  sequences
   recognized by xterm(1):

   ESC ] 0 ; txt ST        Set icon name and window title to txt.

That you don't see any changes is probably because your prompt sets the title to the default title string on returning to the prompt. Try:

 PROMPT_COMMAND= ; printf '\033]0;Hello World!\007'
Related Question