Linux 行尾(新行)在 bash 中转义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16904064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
End of line (new line) escapes in bash
提问by Yoland
Escape character (\
) can be used to escape end of line, e.g.
转义字符 ( \
) 可用于转义行尾,例如
% echo This could be \
a very \
long line\!
This could be a very long line!
%
however, isn't end of line (new line) represented by \n
which has two characters. shouldn't the result of the escape be the literal of \n
. e.g.
但是,不是由\n
which表示的行尾(新行)有两个字符。转义的结果不应该是\n
. 例如
%echo $'\n'
\n
%
Thank you for your answer!
谢谢您的回答!
Edit:
Sorry, I didn't explain it well enough. I am not trying to echo a new line. I am wondering why \
is able to new line character (\n
) which has two character instead of just escape the backslash in the new line character and produce the literal of \n
编辑:对不起,我没有很好地解释它。我不是要回应一个新行。我想知道为什么\
能够换行符 ( \n
) 有两个字符,而不是仅仅转义换行符中的反斜杠并产生 \n 的文字
采纳答案by Markku K.
Actually, \n
is not reallya newline character -- it is an escape sequence that representsa newline (which is just one character in Linux). The \
at the end of a line escapes the actualnewline character that you type in using the enter key. You can look at what ASCII values represent different characters using hexdump:
实际上,\n
它并不是真正的换行符——它是一个表示换行符的转义序列(在 Linux 中只是一个字符)。在\
一行的末尾逃脱实际您在使用回车键输入换行符。您可以使用hexdump查看哪些 ASCII 值代表不同的字符:
%echo $'\n'
\n
%echo $'\n' | hexdump -C
00000000 5c 6e 0a |\n.|
00000003
You will notice that echo printed out 3 characters: \
(5c), n
(6e), and a newline (0a). You will also notice that on the right hand side of the hexdump output, newline shows up as a ".", because it is considered a non-printing character.
您会注意到 echo 打印出 3 个字符:\
(5c)、n
(6e) 和一个换行符 (0a)。您还会注意到,在 hexdump 输出的右侧,换行符显示为“.”,因为它被视为非打印字符。
回答by Nicola Musatti
Newline is the name given in the UNIX world to a character that ends a line in a line-oriented file (or in a terminal). In the UNIX/Linux world this corresponds to the ASCII linefeed character.
换行符是 UNIX 世界中给在面向行的文件(或终端)中结束一行的字符的名称。在 UNIX/Linux 世界中,这对应于 ASCII 换行符。
Different systems use different conventions to end lines: Windows uses a sequence of carriage return and line feed, while Mac originally used a single carriage return. This confusion stems from the fact that these were originally commands needed to move a printer's print head to the beginning of a new line.
不同的系统使用不同的约定来结束行:Windows 使用回车和换行的序列,而 Mac 最初使用单个回车。这种混淆源于这样一个事实,即这些最初是将打印机的打印头移动到新行开头所需的命令。
\n
is a conventional way of expressing the end of line character in code, again originally in the UNIX world, more precisely in the C language. Note that when reading a text file C reads a single newline character even on systems where this is really a two character sequence.
\n
是在代码中表示行尾字符的一种传统方式,同样最初是在 UNIX 世界中,更准确地说是在 C 语言中。请注意,即使在实际上是两个字符序列的系统上,读取文本文件时 C 也会读取单个换行符。