如图所示。所有命令都是相似的。 我知道如何使用它,但我真的不知道细节。 有人会知道吗?非常感谢你。
# does `cat` read fd and print?
$ cat file
# does `cat` read from stdin and print?
$ cat < file
$ cat - < file
# with heredoc or herestring, what methods `cat` command use to read from heredoc?stdin?
$ cat << EOF
heredoc> test
heredoc> EOF
test
$ cat <<< "test"
$ cat - << EOF
heredoc> test
heredoc> EOF
test
$ cat - <<< "test"
# and I dont why these commands works?
$ cat <(echo "test")
$ cat - <<(echo "test")
# why this command doesn't work?
$ cat - <(echo "test")
最佳答案
一些阅读 Material ,全部来自非常有用的 Bash 手册:
Redirection (
<filename
) -- 将标准输入重定向到文件filename
Here documents (
<<WORD
) -- 使标准输入从下一行重定向到脚本源,直到但不包括行WORD
Here strings (
<<<"string"
) -- 将标准输入重定向到字符串string
(好像字符串被写入一个临时文件,然后标准输入被重定向到该文件)Process substitution (
<(command)
) -- 启动一个执行command
的进程并在命令行中插入一个名称,其作用类似于文件名,以便从该"file"读取产生命令的输出
-
的使用指示源文件是标准输入,这是许多命令所共有的,也是 Posix 推荐的。如果没有指定文件,许多命令从标准输入读取。一些,比如 cat
, 实现两种表明意图是从标准输入读取的方式。
请注意 -
和 <(command)
都是文件名参数,而 <filename
, <<WORD
和 <<<"string"
是重定向。因此,虽然它们表面上看起来很相似,但在本质上却大不相同。它们的共同点是它们都与输入有关;其中一些(但不是此处的文档/字符串)具有与输出有关的类似物,使用 >
而不是 <
.
关于linux - 对 shell 中的一些标准输入或 heredoc 用法感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41646782/