This question already has an answer here:
这个问题在这里已有答案:
- Capturing multiple line output into a Bash variable 6 answers
- 捕获多行输出到Bash变量6答案
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?
看起来新行字符正在以某种方式被删除。我如何让他们说出来?
1 个解决方案
#1
18
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 $stuff
into 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扩展为以空格分隔的单词列表(其中'words'是非空格字符的序列,空格字符是空格和制表符和换行符;在实验时,它似乎形式饲料,回车和后退空间不算作空间)。
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为tab,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:
$
#1
18
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 $stuff
into 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扩展为以空格分隔的单词列表(其中'words'是非空格字符的序列,空格字符是空格和制表符和换行符;在实验时,它似乎形式饲料,回车和后退空间不算作空间)。
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为tab,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:
$