bash 管道不适用于 echo 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35116699/
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
Piping not working with echo command
提问by Elliot A.
When I run the following Bash
script, I would expect it to print Hello
. Instead, it prints a blank line and exits.
当我运行以下Bash
脚本时,我希望它打印Hello
. 相反,它打印一个空行并退出。
echo 'Hello' | echo
Why doesn't piping
output from echo
to echo
work?
为什么不piping
从输出echo
到echo
工作?
回答by iobender
echo
prints all of its arguments. It does not read from stdin
. So the second echo
prints all of its arguments (none) and exits, ignoring the Hello
on stdin
.
echo
打印其所有参数。它不从stdin
. 所以第二个echo
打印它的所有参数(无)并退出,忽略Hello
on stdin
。
For a program that reads its stdin
and prints that to stdout
, use cat
:
对于读取它stdin
并将其打印到 的程序stdout
,请使用cat
:
$ echo Hello | cat
Hello
回答by cdarke
In this case the pipe you are using are more correctly known as anonymouspipes, because they have no name (there are also named pipes). Anonymous pipes only work between related processes, for example processes with the same parent.
在这种情况下,您使用的管道更准确地称为匿名管道,因为它们没有名称(也有命名管道)。匿名管道仅在相关进程之间工作,例如具有相同父进程的进程。
Pipes are part of the IO system resulting from the C runtime-library. These streamsare buffered (there is an exception) by default. Basically a pipe is just connecting the output buffer from one process to the input buffer of another.
管道是由 C 运行时库产生的 IO 系统的一部分。默认情况下,这些流被缓冲(有一个例外)。基本上,管道只是将一个进程的输出缓冲区连接到另一个进程的输入缓冲区。
The first three streams used (called file descriptors) are numbered 0, 1, and 2. The first, 0, is known as standard input, or stdin
(the name used in C). By default this is connected to the keyboard, but it can be redirected either using the <
symbol or the program name being on the right side of a pipe.
使用的前三个流(称为文件描述符)编号为 0、1 和 2。第一个 0 称为标准输入,或stdin
(C 中使用的名称)。默认情况下,它连接到键盘,但可以使用<
符号或管道右侧的程序名称对其进行重定向。
The second, 1, is known as standard output, or stdout
. By default this is connected to the terminal screen, but can be redirected by using the >
symbol or the program name being on the left side of a pipe.
第二个 1 称为 标准输出,或stdout
。默认情况下,它连接到终端屏幕,但可以使用>
管道左侧的符号或程序名称重定向。
So:
所以:
echo 'Hello' | echo
takes the standard output from echo
and passes it to the standard input of echo
. But echo
does not read stdin! So nothing happens.
从 中获取标准输出echo
并将其传递给 的标准输入echo
。但echo
不读取标准输入!所以什么都不会发生。
Filter programsprocess the filenames specified on the command-line. If no filenames are given then they read stdin. Examples include cat
, grep
, and sed
, but notecho
. For example:
过滤程序处理命令行上指定的文件名。如果没有给出文件名,则它们读取标准输入。示例包括cat
、grep
、 和sed
,但不包括echo
。例如:
echo 'Hello' | cat
will display 'Hello', and the cat
is useless (it often is).
将显示“你好”,并且cat
没有用(通常是)。
echo 'Hello' | cat file1
will ignorethe output from echo
and just display the contents of file1. Remember that stdin is only read if no filename is given.
将忽略输出echo
并只显示 file1 的内容。请记住,只有在没有给出文件名时才会读取 stdin。
What do you think this displays?
你认为这显示了什么?
echo 'Hello' | cat < file1 file2
and why?
为什么?
Finally, the third stream, 2, is called standard error, or stderr
, and this one is unbuffered. It is ignored by pipes, because they only operate between stdin and stdout. However, you can redirect stderr to use stdout (see man dup2
):
最后,第三个流 2 被称为标准错误,或者stderr
,这个流是unbuffered。它被管道忽略,因为它们只在 stdin 和 stdout 之间运行。但是,您可以重定向 stderr 以使用 stdout(请参阅 参考资料man dup2
):
myprog 2>&1 | anotherprog
The 2>&1
means "redirect file descriptor 2 to the same place as fie descriptor 1".
的2>&1
手段“重定向文件描述符2向相同的位置FIE描述符1”。
The above is normal behaviour, however a program can override all that if it wants to. It could read from file descriptor 2, for example. I have omitted a lot of other detail, including other forms of redirection such as process substitutionand here documents.
以上是正常行为,但是程序可以根据需要覆盖所有这些。例如,它可以从文件描述符 2 中读取。我省略了很多其他细节,包括其他形式的重定向,例如进程替换和此处的文档。
回答by Rakesh Nair
Piping can be done only for commands taking inputs from stdin. But echo does not takes from stdin. It will take input from argument and print it. So this wont work. Inorder to echo you can do something like echo $(echo 'hello')
管道只能对从 stdin 获取输入的命令进行。但是 echo 不会从标准输入中获取。它将从参数中获取输入并打印它。所以这行不通。为了回声,你可以做类似的事情echo $(echo 'hello')
回答by anubhava
It is because echo
(both builtin and /bin/echo
) don't read anything from stdin
.
这是因为echo
(内置和/bin/echo
)不从stdin
.
Use cat
instead:
使用cat
来代替:
echo 'Hello' | cat
Hello
Or without pipes:
或者没有管道:
cat <<< 'Hello'