I recently read an article about shell metacharacters by Opensource.com correspondent Don Watkins. His article made me think about all the weird things you could do with shell input. While I probably have yet to discover the extremes, I do often find shell escape sequences, like \b
and \t
and \f
strangely useful.
Escape sequences are a special type of terminal input. They're designed to make it possible for you to enter characters or events that you may not have on your physical keyboard. Here are my favorite escape sequences for the Bash shell.
1. Backspace
You can enter a backspace character as part of a command, more or less loading it to trigger once the command executes. For instance, looking casually at this command, you might expect its output to be ab
, but take a look at the actual output:
$ echo a$'\b'b
b
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
, and so the viewable output is just b
.
2. 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. Look at the difference between a printf
statement without the \n
newline character and one with it:
$ printf "%03d.txt" 1
001.txt$
$ printf "%03d.txt\n" 1
001.txt
$
3. Form feed
A \f
form feed signal is like a newline character, but without the imperative to return to column 0. Here's a 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.
4. 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
5. Unicode
There are a lot of characters available in the Unicode standard, and your keyboard only has about 100 keys. There are a few ways to enter special characters on Linux, but one way to enter them into the terminal is to use the Unicode escape sequence. You 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 $'\u270B'
✋
$ echo $'\u2658'
♘
$ echo $'\u2B67'
⭧
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 5-of-Hearts playing card only appears with the \U
escape sequence:
$ echo $'\U1F0B5'
🂵
Have a look around on the Unicode specification to find your niche, and use \u
and \U
to access all the special symbols you need.
Escape the shell
There are 18 escape sequences listed in the man page for the Bash shell, and some I find more useful than others. I've covered my favorites in this article, and Don Watkins talked about the metacharacters he uses most often in his article, yet there's still more to be discovered. There are ways to encode ranges of letters and numbers, subshells, mathematical equations, and more. For a good overview of metacharacters available for the shell, download our metacharacter cheat sheet and keep it handy as you get better at using the most powerful application on your computer: the Linux terminal.
2 Comments