translated

This commit is contained in:
Tao Zhou 2023-03-03 21:54:33 +08:00 committed by GitHub
parent 793c7d0e0f
commit 0af06ecb9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 121 additions and 121 deletions

View File

@ -1,121 +0,0 @@
[#]: subject: "5 escape sequences for your Linux shell"
[#]: via: "https://opensource.com/article/23/2/escape-sequences-linux-shell"
[#]: author: "Seth Kenlon https://opensource.com/users/seth"
[#]: collector: "lkxed"
[#]: translator: "zepoch"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
5 escape sequences for your Linux shell
======
I recently read an [article about shell metacharacters][1] 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][2], 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][3] 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/][4].
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][5] and keep it handy as you get better at using the most powerful application on your computer: the Linux terminal.
--------------------------------------------------------------------------------
via: https://opensource.com/article/23/2/escape-sequences-linux-shell
作者:[Seth Kenlon][a]
选题:[lkxed][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/seth
[b]: https://github.com/lkxed/
[1]: https://opensource.com/article/22/2/metacharacters-linux
[2]: https://opensource.com/article/20/8/printf
[3]: https://opensource.com/article/22/7/linux-compose-key-cheat-sheet
[4]: https://www.unicode.org/charts/
[5]: https://opensource.com/downloads/linux-metacharacters-cheat-sheet

View File

@ -0,0 +1,121 @@
[#]: subject: "5 escape sequences for your Linux shell"
[#]: via: "https://opensource.com/article/23/2/escape-sequences-linux-shell"
[#]: author: "Seth Kenlon https://opensource.com/users/seth"
[#]: collector: "lkxed"
[#]: translator: "zepoch"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
5 对你有帮助的 Linux shell 的转义序列
======
我最近在读一篇[关于 shell 字符的文章][1],这篇文章的作者是 Opensource.com 网站的通讯作者 Don watkins。他的文章让我想到了你可以用 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][2] 这样的命令时,它不会像 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. 制表符
There are two tab escape sequences: the `\t` horizontal tab and the `\v` vertical tab. The horizontal tab is exactly what you'd expect.有两种制表符转义序列:水平制表符 `\t` 和垂直制表符 `v`。水平制表符如下所示。
```
$ echo a$'\t'b
a b
```
理论上,垂直制表符是相同的原理,但是在垂直空间中。然而,在大多数控制台上,一行的垂直间距是不可变的,所以它通常看起来很像一个换页符:
```
$ echo a$'\v'b
a
b
```
### 5. Unicode
Unicode标准中有很多可用的字符而您的键盘只有大约100个键。在Linux上有几种方法可以输入[特殊字符][3],但是将它们输入到终端的一种方法是使用 Unicode 转义序列。这个转义序列以 `\u` 开头,后跟一个十六进制值。您可以在文件 `/usr/share/X11/locale/en_US.UTF-8/Compose` 中找到许多Unicode值。您也可以在 [https://www.Unicode.org/charts/][4] 查看 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 可用的元字符,可以下载我们的[元字符备忘表][5]使得您可以更好地使用计算机上最强大的应用程序——Linux终端时将它放在手边。
--------------------------------------------------------------------------------
via: https://opensource.com/article/23/2/escape-sequences-linux-shell
作者:[Seth Kenlon][a]
选题:[lkxed][b]
译者:[zepoch](https://github.com/zepoch)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/seth
[b]: https://github.com/lkxed/
[1]: https://opensource.com/article/22/2/metacharacters-linux
[2]: https://opensource.com/article/20/8/printf
[3]: https://opensource.com/article/22/7/linux-compose-key-cheat-sheet
[4]: https://www.unicode.org/charts/
[5]: https://opensource.com/downloads/linux-metacharacters-cheat-sheet