Linux 包含命令输出的变量的 Shell 脚本打印内容删除换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1483721/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:44:01  来源:igfitidea点击:

Shell script printing contents of variable containing output of a command removes newline characters

linuxshellecho

提问by

I'm writing a shell script which will store the output of a command in a variable, process the output, and later echo the results. Here's what I've got:

我正在编写一个 shell 脚本,它将命令的输出存储在一个变量中,处理输出,然后回显结果。这是我所拥有的:

stuff=$(diff -u pens tape)
# process the output
echo $stuff

The problem is, the output I get from running the script is this:

问题是,我从运行脚本中得到的输出是这样的:

--- pens 2009-09-27 10:29:06.000000000 -0400 +++ tape 2009-09-18 16:45:08.000000000 -0400 @@ -1,4 +1,2 @@ -highlighter -marker -pencil -POSIX +masking +duct

Whereas I was expecting this:

而我期待这个:

--- pens 2009-09-27 10:29:06.000000000 -0400
+++ tape 2009-09-18 16:45:08.000000000 -0400
@@ -1,4 +1,2 @@
-highlighter
-marker
-pencil
-POSIX
+masking
+duct

It looks like the newline characters are being removed somehow. How do I get them to say in?

看起来换行符正在以某种方式被删除。我如何让他们说出来?

采纳答案by Jonathan Leffler

If you want to preserve the newlines, enclose the variable in double quotes:

如果要保留换行符,请将变量括在双引号中:

echo "$stuff"

When you write it without the double quotes, the shell expands $stuffinto a space-separated list of words (where 'words' are sequences of non-space characters, and the space characters are blanks and tabs and newlines; upon experimentation, it seems that form feeds, carriage returns and back-spaces are not counted as space).

当你在没有双引号的情况下编写它时,shell 扩展$stuff为一个以空格分隔的单词列表(其中“单词”是非空格字符序列,空格字符是空格、制表符和换行符;在实验中,似乎换页、回车和退格不计为空格)。



Demonstrating interpretation of control characters as white space. ASCII 8 is backspace, 9 is tab, 10 is new line (LF), 11 is vertical tab, 12 is form feed, 13 is carriage return. The first command generates a sequence of characters separated by the various control characters. The second command echoes with the result with the original characters preserved - see the hex dump. The third command echoes the result with the shell splitting the words; you can see that the tab and newline were replaced by blank (0x20).

演示将控制字符解释为空格。ASCII 8 是退格,9 是制表符,10 是换行符 (LF),11 是垂直制表符,12 是换页符,13 是回车符。第一个命令生成由各种控制字符分隔的字符序列。第二个命令与保留原始字符的结果相呼应 - 请参阅十六进制转储。第三个命令用shell拆分单词来回显结果;您可以看到制表符和换行符被替换为空白 (0x20)。

$ x=$(./ascii 64 65 8 66 67 9 68 69 10 70 71 11 72 73 12 74 75 13 76 77)
$ echo "$x" | odx
0x0000: 40 41 08 42 43 09 44 45 0A 46 47 0B 48 49 0C 4A   @A.BC.DE.FG.HI.J
0x0010: 4B 0D 4C 4D 0A                                    K.LM.
0x0015:
$ echo  $x  | odx
0x0000: 40 41 08 42 43 20 44 45 20 46 47 0B 48 49 0C 4A   @A.BC DE FG.HI.J
0x0010: 4B 0D 4C 4D 0A                                    K.LM.
0x0015:
$