5 escape sequences for your Linux shell

Use these secret codes for your Bash terminal. Download our new Linux metacharacters cheat sheet.
8 readers like this.
bash logo on green background

Opensource.com

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.

What to read next
Tags
Seth Kenlon
Seth Kenlon is a UNIX geek, free culture advocate, independent multimedia artist, and D&D nerd. He has worked in the film and computing industry, often at the same time.

2 Comments

Nice beginner information. I prefer to use "echo -e" so that you don't need to add the $ in front of the metachararacter. Also, I use \xHH to do hex. Plus, I find hexdump (typically 'hexdump -C') to check that things are what I expect them to be, or to look at something that might have unprintable characters embedded in them.

$ echo a $'\b'b |hexdump -C
00000000 61 20 08 62 0a |a .b.|
00000005

$ echo -e "a \bb" |hexdump -C
00000000 61 20 08 62 0a |a .b.|
00000005

I think the "echo -e" is just a little cleaner, more obvious method.

Loved this article! Many of these escape sequences originate from when Unix terminals were Teletype (large rolls of paper instead of computer screens). I also used these in the 1980s and 1990s when I used a dot matrix printer.

The \f formfeed tells the printer to skip ahead to the next page. Printed pages had 66 lines per page, so if you were on line 10, it was faster to have the printer automatically eject the page with formfeed than to give the printer 56 blank lines.

The \v vertical tab was similar; I recall that the old paper terminal we had at university would divide the page into sections, so a vertical tab would skip to, say, the next "1-inch" start. Other printers could be programmed with different vertical tabs, like one "tab" to put you in the page header, another "tab" to put you in the body, and another "tab" for the footer The vertical tab is faster than giving the printer some blank lines to move to the right spot.

Same for \t horizontal tabs. With a horizontal tab, the printer head skips ahead to the next tab position. And especially with older printers, using a horizontal tab was faster than feeding the printer some spaces.

Using \b backspace and typing the same letter was a common way to create a boldface effect on a standard Teletype or similar printing device.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.