Bash/Python:当作为另一个命令的输入时,`echo` 命令会插入换行符吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15728703/
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
Bash/Python: does `echo` command insert a newline when acting as input to another command?
提问by Bryce Thomas
I have a simple python script line_printer.py:
我有一个简单的 python 脚本line_printer.py:
import fileinput
i = 1
for line in fileinput.input():
print 'Line', str(i), ':', line.strip()
i+=1
I'm trying to understand how piping data in from the echocommand to this script affects the result versus reading the data in from file.
我试图了解从echo命令到此脚本的管道数据如何影响结果,而不是从文件中读取数据。
Consider the following call and output:
考虑以下调用和输出:
$ echo -e "chicken\ncow\npig"
chicken
cow
pig
$
This to me looks like echohas appended an invisble \nafter the "g" in pig. So, how come when I call:
这对我来说看起来像是在猪的“g”之后echo附加了一个隐形\n。那么,当我打电话时怎么会:
echo -e "chicken\ncow\npig" | python line_printer.py
I get:
我得到:
Line 1 : chicken
Line 2 : dog
Line 3 : cow
as the output and not:
作为输出而不是:
Line 1 : chicken
Line 2 : dog
Line 3 : cow
Line 4 :
At first I thought the behaviour of Python's fileinputmodule might be to discard the final line in a file if it is blank. But when I try using the contents of a file some_lines.txt:
起初我认为 Pythonfileinput模块的行为可能是丢弃文件中的最后一行,如果它是空白的。但是当我尝试使用文件的内容时some_lines.txt:
chicken
dog
cow
<blank line>
as the input:
作为输入:
python line_printer.py some_lines.txt
The output I get is:
我得到的输出是:
Line 1 : chicken
Line 2 : dog
Line 3 : cow
Line 4 :
So why does line_printer.pygive different results on the input depending on whether it originated from stdin versus originated from a file? Best I can tell, both stdin (from echo) and the file (from some_lines.txt) finish with a \n, so I would either expect the output of both to include the Line 4 :or the output of neither to include it.
那么为什么line_printer.py根据输入是源自 stdin 还是源自文件而给出不同的结果呢?我能说的最好的是,标准输入 (from echo) 和文件 (from some_lines.txt) 都以 a 结尾\n,所以我要么期望两者的输出都包含 要么 两者Line 4 :的输出都不包含它。
回答by jim mcnamara
This command will answer your question:
此命令将回答您的问题:
echo 'hi' | od -c
The reason for the trailing \ncharacter is that stdout on a terminal by default uses line buffering - meaning it will only display output data that ends with the newline character.
尾随\n字符的原因是默认情况下终端上的 stdout 使用行缓冲 - 这意味着它只会显示以换行符结尾的输出数据。
Play around with the printf command:
玩弄 printf 命令:
printf "%s" foo
printf "%s\n" anotherfoo
回答by Jonathan Ben-Avraham
If you look in the bash source, bash-4.2/builtins/echo.def you can see that the builtin echocommand always (line 113) outputs a final \n(line 194) unless the -nwas specified (line 198) or output of echois used as a string (line 166). You can test this by doing
如果您查看 bash 源代码 bash-4.2/builtins/echo.def,您可以看到内置echo命令始终(第 113 行)输出最终\n(第 194 行),除非-n指定了(第 198 行)或使用的输出echo作为一个字符串(第 166 行)。你可以通过这样做来测试
echo `echo "Ho ho ho"` | od -c
You will see only one \nbecause the output of echo "Ho ho ho"is evaluated as a string in the expression echo `echo "Ho ho ho"`.
您将只看到一个,\n因为 的输出echo "Ho ho ho"被评估为表达式中的字符串echo `echo "Ho ho ho"`。
It doesn't seem to have any relation to the terminal setup.
它似乎与终端设置没有任何关系。

