5 个有用的 Linux Shell 转义序列
转义序列
![](/data/attachment/album/202303/05/134703ne9559n9rmrer6ee.jpg)



> 
> 如何在你的 Bash 终端使用这些秘密代码，请下载我们新的 Linux 元字符速查表。
> 
> 
> 


我最近在读一篇 Don watkins [关于 Shell 元字符的文章](https://opensource.com/article/22/2/metacharacters-linux)。他的文章让我想到了你可以用 shell 输入做的所有的奇怪事情。虽然我可能还没有发现极端的情况，但是我经常发现 shell 转义序列，比如 `\b`、`\t` 和 `\f` 非常有用。


转义序列是一种特殊类型的终端输入。它们旨在让你能够输入物理键盘上没有的字符或触发事件。下面是我最喜欢的 Bash shell 的转义序列。


### 1、退格符


你可以在命令中输入若干退格符，以便在命令执行时触发。例如这个命令，你可能会认为它的输出是`ab`，但是看一下真正的输出：



```
$ echo a$'\b'b
b

```

从技术上来说，Shell 确实输出了 `ab`（你可以通过在命令后面附加 `| wc -m` 来确认这一点），但是全部输出的一部分是 `\b` 退格事件。退格键在输出 `b` 字符之前删除了 `a` 字符，因此输出只有 `b` 字符。


### 2、换行符


换行符是一个让你的 Shell 转到下一行的第 0 列的信号。这一点很重要，当使用像 [printf](https://opensource.com/article/20/8/printf) 这样的命令时，它不会像 `echo` 那样在输出的末尾自动添加换行符。看看不带 `\n` 换行符的 `printf` 语句和带换行符的 `printf` 语句之间的区别：



```
$ printf "%03d.txt" 1
001.txt$
$ printf "%03d.txt\n" 1
001.txt
$

```

### 3、换页符


`\f` 换页信号就像换行符，但是却并不是返回到第 0 列。下面是一个使用换页符而不是换行符的 `printf` 命令:



```
$ printf "%s\f" hello
hello
     $

```

你的 Shell 提示符出现在下一行，但不是在下一行的行首。


### 4、制表符


有两种制表符转义序列：水平制表符 `\t` 和垂直制表符 `\v`。水平制表符如下所示：



```
$ echo a$'\t'b
a     b

```

理论上，垂直制表符是相同的原理，但是在垂直空间中。然而，在大多数控制台上，一行的垂直间距是不可变的，所以它通常看起来很像一个换页符：



```
$ echo a$'\v'b
a
 b

```

### 5、Unicode


Unicode 标准中有很多可用的字符，而你的键盘只有大约 100 个键。在 Linux 上有几种方法可以输入 [特殊字符](https://opensource.com/article/22/7/linux-compose-key-cheat-sheet)，但是将它们输入到终端的一种方法是使用 Unicode 转义序列。这个转义序列以 `\u` 开头，后跟一个十六进制值。你可以在文件 `/usr/share/X11/locale/en_US.UTF-8/Compose` 中找到许多 Unicode 值。你也可以在 [https://www.Unicode.org/charts/](https://www.unicode.org/charts/) 查看 Unicode 规范。


这对于输入像圆周率 π（圆的周长与直径之比）等常见符号非常有用：



```
$ echo $'\u03C0'
π

```

还有许多其他的符号和字符：



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

```

有盲文符号、音乐符号、字母、电气符号、数学符号、表情符号、游戏符号等等。事实上，有如此多的可用符号，有时你需要 `\U`（注意大写字母）Unicode 转义序列来访问高区的 Unicode。例如，这张红心 5 的扑克牌只出现在 `\U` 转义序列中:



```
$ echo $'\U1F0B5'
?

```

浏览一下 Unicode 规范，找到适合你的位置，并使用 `\u` 和 `\U` 来访问你需要的所有特殊符号。


### 除此之外


Bash Shell 的手册页中列出了 18 个转义序列，我发现其中一些更有用。我已经在本文中介绍了我最爱的几个，Don Watkins 也谈到了他在文章中最常用的元字符，但是还有更多待发现。有很多方法可以对字母和数字、子 Shell、数学方程等进行编码。为了更好地了解 Shell 可用的元字符，可以下载我们的 [元字符速查表](https://opensource.com/downloads/linux-metacharacters-cheat-sheet)，你可以在使用计算机上最强大的应用程序 —— Linux 终端时将它放在手边。




---


via: <https://opensource.com/article/23/2/escape-sequences-linux-shell>


作者：[Seth Kenlon](https://opensource.com/users/seth) 选题：[lkxed](https://github.com/lkxed/) 译者：[zepoch](https://github.com/zepoch) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
I recently read an [article about shell metacharacters](https://opensource.com/article/22/2/metacharacters-linux) 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](https://opensource.com/article/20/8/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](https://opensource.com/article/22/7/linux-compose-key-cheat-sheet) 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/](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](https://opensource.com/downloads/linux-metacharacters-cheat-sheet) and keep it handy as you get better at using the most powerful application on your computer: the Linux terminal.

## 2 Comments