Do terminals ignore superfluous escape sequences silently?


printf "\033[?25l"
printf "\033[?25l"  # superfluous
printf "\033[31m"
printf "\033[31m"   # superfluous
printf "Red Text\n"
sleep 1
printf "\033[0m"
printf "\033[?25h"

1 Answer 1


Yes, they do.

You might be familiar with certain document formats, e.g. HTML where opening and closing tags need to correspond to each other. E.g. this is okay:

<font style="color:red"><font style="color:red">foobar</font></font>

but this is not, since the tags are not balanced:

<font style="color:red"><font style="color:red">foobar</font>

Terminal emulation is not like this. The terminal emulator doesn't see an entire "document" at once, rather it sees an incoming stream as it arrives over time.

Terminal emulators are a state machine. Complete escape sequences, such as \033[?25l or \033[31m do not open or close contexts, they just set a certain new state. The first example here makes the cursor invisible, the second one switches to red font. You might execute them multiple times, the cursor doesn't become any more invisible and the text doesn't become any more red, nor will there be instances of these instructions remaining somewhere on the stack. These are one-time assignments: the cursor is made invisible (no matter what its previous state was), and the color for printing subsequent text is switched to red (again: no matter what its previous state was).

There are only a few escape sequences which have somewhat of an opening-closing semantics, e.g. you can push the current window title on a stack and later pop it from there, obviously these are supposed to be used in pairs.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .