Shell

5 essential escape characters in Linux Shell

5 essential escape characters in Linux Shell

Escape sequences are an invaluable tool for entering special characters and events that cannot be found on a standard keyboard. BASH shell is particularly rich in escape sequences, some of which are more useful than others. In this article, we will discuss five of the most essential escape sequences, which can be highly beneficial to know.

Whether it's for making a script more efficient, debugging, or just for making life easier, these five escape sequences are essential tools to have in any programmer's toolkit. From the simple yet helpful \t tab character, to the complex \u unicode character, these escape sequences provide a wide range of capabilities to help make your work easier.

Backspace

A backspace character can be entered as part of a command to trigger once the command executes. For example, running the command:

$ echo a$'\b'b
b

The expected output would be ab, but you'll see b instead. Although technically, the shell did output ab (you can confirm that by appending | wc -m to the command), but part of the total output was the \b backspace event. The backspace removed a before outputting b, so the viewable output is just b.

Newline

A newline character is a signal for your shell to go to column 0 of the next line. This is essential when using a command like printf, which doesn't assume that you want a newline added to the end of your output, the way echo does. For example, compare the difference between a printf statement without the \n newline character and one with it:

$ printf "%03d.txt" 25
025.txt$
$ printf "%03d.txt\n" 2
002.txt

Form feed

A \f form feed signal is like a newline character, but without the imperative to return to column 0. For instance, the printf command using a form feed instead of a newline:

$ printf "%s\f" hello
hello
     $

Your shell prompt is on the next line, but not at the start of the line.

Tab

There are two tab escape sequences: the \t horizontal tab and the \v vertical tab. The horizontal tab is exactly what you'd expect.

$ echo a$'\t'b
a     b

The vertical tab is, in theory, the same principle but in vertical space. On most consoles, though, the vertical spacing of a line isn't variable, so it usually ends up looking a lot like a form feed:

$ echo a$'\v'b
a
 b

Unicode

The Unicode standard has many available characters, and your keyboard only has about 100 keys. One way to enter special characters on Linux is to use the Unicode escape sequence. You can start this escape sequence with \u followed by a hexadecimal value. You can find many Unicode values in the file /usr/share/X11/locale/en_US.UTF-8/Compose, or you can look at the Unicode specification at https://www.unicode.org/charts/.

This can be a useful trick for entering common symbols like Pi (the ratio of a circle's circumference to its diameter):

$ echo '\u03C0'
π

There are lots of other symbols and characters, too.

$ echo '\u2709'
✉
$ echo '\u2601'
☁
$ echo '\u231B'
⌛

There's Braille notation, musical notation, alphabets, electrical symbols, mathematical symbols, emoji, game symbols, and much more. In fact, there are so many available symbols that sometimes you need the \U (note the capital letter) Unicode escape sequence to access Unicode in the high ranges. For instance, this rocket emoji only appears with the \U escape sequence:

$ echo '\U1F680'
🚀
A blog for self-taught engineers

Сommunity is filled with like-minded individuals who are passionate about learning and growing as engineers.