简体   繁体   English

行尾(新行)以bash格式转义

[英]End of line (new line) escapes in bash

Escape character ( \\ ) can be used to escape end of line, eg 转义字符( \\ )可用于转义行尾,例如

% 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. 但是,它不是由\\n表示的行尾(换行符),它有两个字符。 shouldn't the result of the escape be the literal of \\n . 不应该逃脱的结果是\\n的文字。 eg 例如

%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的字面值

Actually, \\n is not really a newline character -- it is an escape sequence that represents a newline (which is just one character in Linux). 事实上, \\n是不是一个真正的换行符-它是表示一个换行符(这是Linux的一个字符)的转义序列。 The \\ at the end of a line escapes the actual newline character that you type in using the enter key. 行末尾的\\转义使用回车键输入的实际换行符。 You can look at what ASCII values represent different characters using hexdump : 您可以使用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). 您会注意到回声打印出3个字符: \\ (5c), n (6e)和换行符(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 . 您还会注意到,在hexdump输出的右侧,换行符显示为“。”,因为它被视为非打印字符

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). Newline是UNIX世界中给出的一个字符的名称,该字符在面向行的文件(或终端)中结束一行。 In the UNIX/Linux world this corresponds to the ASCII linefeed character. 在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. 不同的系统对结束行使用不同的约定:Windows使用一系列回车和换行,而Mac最初使用单回车。 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. 这种混淆源于这样的事实,即这些原本是将打印机的打印头移动到新行开头所需的命令。

\\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. \\n是在代码中表达行尾字符的传统方式,最初在UNIX世界中,更确切地说在C语言中。 Note that when reading a text file C reads a single newline character even on systems where this is really a two character sequence. 请注意,在读取文本文件时,即使在实际上是两个字符序列的系统上,C也会读取单个换行符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM