Science and technology

5 escape sequences on your Linux shell

I lately learn an article about shell metacharacters by Opensource.com correspondent Don Watkins. His article made me take into consideration all of the bizarre issues you possibly can do with shell enter. While I in all probability have but to find the extremes, I do usually discover shell escape sequences, like b and t and f surprisingly helpful.

Escape sequences are a particular sort of terminal enter. They’re designed to make it attainable so that you can enter characters or occasions that you could be not have in your bodily keyboard. Here are my favourite escape sequences for the Bash shell.

1. Backspace

You can enter a backspace character as a part of a command, roughly loading it to set off as soon as the command executes. For occasion, wanting casually at this command, you would possibly count on its output to be ab, however check out the precise output:

$ echo a$'b'b
b

Technically, the shell did output ab (you’ll be able to verify that by appending | wc -m to the command) however a part of the full output was the b backspace occasion. The backspace eliminated a earlier than outputting b, and so the viewable output is simply b.

2. Newline

A newline character is a sign on your shell to go to column 0 of the following line. This is crucial when utilizing a command like printf, which does not assume that you really want a newline added to the top of your output, the best way echo does. Look on the distinction between a printf assertion with out the n newline character and one with it:

$ printf "%03d.txt" 1
001.txt$
$ printf "%03d.txtn" 1
001.txt
$

3. Form feed

A f kind feed sign is sort of a newline character, however with out the crucial to return to column 0. Here’s a printf command utilizing a kind feed as an alternative of a newline:

$ printf "%sf" hiya
hiya
     $

Your shell immediate is on the following line, however not at first of the road.

4. Tab

There are two tab escape sequences: the t horizontal tab and the v vertical tab. The horizontal tab is strictly what you’d count on.

$ echo a$'t'b
a     b

The vertical tab is, in idea, the identical precept however in vertical house. On most consoles, although, the vertical spacing of a line is not variable, so it often finally ends up wanting rather a lot like a kind feed:

$ echo a$'v'b
a
 b

5. Unicode

There are quite a lot of characters accessible within the Unicode customary, and your keyboard solely has about 100 keys. There are a number of methods to enter special characters on Linux, however one option to enter them into the terminal is to make use of the Unicode escape sequence. You begin this escape sequence with u adopted by a hexadecimal worth. You can discover many Unicode values within the file /usr/share/X11/locale/en_US.UTF-8/Compose, or you’ll be able to take a look at the Unicode specification at https://www.unicode.org/charts/.

This generally is a helpful trick for getting into widespread symbols like Pi (the ratio of a circle’s circumference to its diameter):

$ echo $'u03C0'
π

There are plenty of different symbols and characters, too.

$ echo $'u270B'
✋
$ echo $'u2658'
♘
$ echo $'u2B67'
⭧

There’s Braille notation, musical notation, alphabets, electrical symbols, mathematical symbols, emoji, sport symbols, and way more. In truth, there are such a lot of accessible symbols that generally you want the U (observe the capital letter) Unicode escape sequence to entry Unicode within the excessive ranges. For occasion, this 5-of-Hearts taking part in card solely seems with the U escape sequence:

$ echo $'U1F0B5'
🂵

Have a go searching on the Unicode specification to seek out your area of interest, and use u and U to entry all of the particular symbols you want.

Escape the shell

There are 18 escape sequences listed within the man web page for the Bash shell, and a few I discover extra helpful than others. I’ve coated my favorites on this article, and Don Watkins talked concerning the metacharacters he makes use of most frequently in his article, but there’s nonetheless extra to be found. There are methods to encode ranges of letters and numbers, subshells, mathematical equations, and extra. For a very good overview of metacharacters accessible for the shell, obtain our metacharacter cheat sheet and preserve it helpful as you get higher at utilizing essentially the most highly effective utility in your laptop: the Linux terminal.

Most Popular

To Top