本文介绍了行尾(新行)以bash格式转义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

转义字符(\)可用于转义行尾,例如

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!
%

但是,不是由\n表示的具有两个字符的行尾(换行).转义的结果不应为 \n 的文字.例如

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.

%echo $'\\n'
\n
%

谢谢您的回答!

抱歉,我的解释不够好.我没有在回声换行.我想知道为什么\能够对具有两个字符的换行符(\n)进行替换,而不仅仅是在换行符中转义反斜杠并生成\ n

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不是真的换行符-它是表示的转义序列换行符(在Linux中只是一个字符).行尾的\会转义您使用Enter键键入的 actual 换行符.您可以使用 hexdump 来查看哪些ASCII值代表不同的字符:

Actually, \n is not really a newline character -- it is an escape sequence that represents a newline (which is just one character in 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:

%echo $'\\n'
\n
%echo $'\\n' | hexdump -C
00000000  5c 6e 0a                   |\n.|
00000003

您会注意到回显打印了3个字符:\(5c),n(6e)和换行符(0a).您还将注意到,在hexdump输出的右侧,换行符显示为.",因为它被视为 non -打印字符.

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.

这篇关于行尾(新行)以bash格式转义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:16