For a list of BASHing data 2 blog posts see the index page. ![]()
THE escape character, not AN escape character
An escape character is any character that tells a computer program or communications protocol that the next character or characters have special meaning. The escape character plus those next meaningful characters are together called an "escape sequence". On the command line, for example, the escape character "\" followed by the "n" character means "newline":
The -e option tells echo to interpret escape sequences.
OK, but there's another escape character, namely 00011011 as an 8-bit binary number, 27 as a decimal, 1b as a hexadecimal and 33 as an octal. This character is part of the basic ASCII character set and has been absorbed into Unicode. It's used in escape sequences, like the ones that colorise text.
In the first command below, I have "[1;31m" telling the BASH built-in printf that the next letter should be red and bold, and "[0m" telling it to stop red-bolding. These aren't escape sequences until I escape them. The correct escape sequence contains THE escape character, but that character also has to be escaped. For example, if I want to represent the escape character with its hexadecimal value 1b, I need to put a backslash and an "x" in front of the 1b to create the escape sequence to introduce the color escape sequences:
Confused? Try this: "\x1b" is an escape sequence within the escape sequence "\x1b[1;31m". The backslash "\" is an escape character for the inner escape sequence "\x1b", and "\x1b" is an escape character for the whole escape sequence.
And you can't type THE escape character by pressing the ESC key on your keyboard. That controls a different kind of escape. (In many programs it stops the currently running action.)
To enter THE escape character in a terminal you need to escape it, usually with a backslash. How you do that depends on the program you're using. With the BASH built-in programs echo and printf, escape sequences with hexadecimal, octal and Unicode ("uNNNN") work, but not decimal:
The GNU printf program understands hexadecimal and octal, but not Unicode or decimal:
The "invalid" message is not a bug, according to the program's author.
GNU sed is happy with all four kinds of escapes, but needs the shell to expand the Unicode version:
GNU AWK also needs help with Unicode, and doesn't understand the decimal escape:
Two other ways to enter THE escape character from a keyboard will work in some programs and some operating systems. These are Ctrl + [ and "\e". The first method doesn't work in any program I've tried. "\e", on the other hand, works fine with BASH built-ins and GNU printf, and with both GNU sed and GNU AWK if expanded by the shell:
"\E" works the same as "\e" with the BASH built-ins, but not with GNU printf.
Versions:
echo in BASH 5.2
printf in BASH 5.2
GNU printf 9.1 in coreutils
GNU sed 4.9
GNU AWK 5.2
Next post:
2025-11-28 Similar to "most distant pair of points", sort of
Last update: 2025-11-21
The blog posts on this website are licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License