5

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?

3

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'
5

Actually, the escape sequence starts with the first escape character and ends with the \033\007 (ASCII ESC and ASCII BEL).

The 0 is a parameter, corresponding to Ps in the description for xterm, while the text after the semicolon is Pt:

        Ps = 0  -> Change Icon Name and Window Title to Pt.
        Ps = 1  -> Change Icon Name to Pt.
        Ps = 2  -> Change Window Title to Pt.

Both \033] and \033\007 are parts of the escape sequence which can be changed to other characters. The terminal treats the pair of bytes as a single control character (just as the 4 bytes \007 are treated by printf as a single character). Referring to XTerm Control Sequences, the section C1 (8-Bit) Control Characters may be helpful:

ESC \
     String Terminator (ST  is 0x9c).
ESC ]
     Operating System Command (OSC  is 0x9d).

in understanding the beginning of Operating System Controls:

OSC Ps ; Pt ST
OSC Ps ; Pt BEL

Using BEL (\007) to end an escape sequence is an anomaly. It does not follow the standard (ECMA-48). Operating system controls should begin with either ESC ] or 0x9d, and end with ESC \ or 0x9c.

Long ago, the developer(s) of xterm added an escape sequence for setting the title. In X11R1 (1987), the program simply read the sequence until it got a nonprinting character. Later, in X11R4 (1989), someone improved this by terminating on a BEL character. The standard had been around longer than that, but the reason for choosing BEL rather than ST is not known. Ultimately that was addressed in the late 1990s, by recognizing either (but keeping BEL as an alternative since many users relied on hardcoded behavior with BEL).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.