linux - 对 shell 中的一些标准输入或 heredoc 用法感到困惑

标签 linux bash shell

enter image description here

如图所示。所有命令都是相似的。 我知道如何使用它,但我真的不知道细节。 有人会知道吗?非常感谢你。

# 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/

相关文章:

c - Linux C-keyMapping keyCodes

php - 如何判断一个查询是否已经结束

c++ - OpenCV 2.4.2 calcOpticalFlowPyrLK 没有找到任何点

linux - 在 linux shell 上显示日志文件的特定部分

bash - 为什么 "set -v"似乎不能在 bash 的子 shell 中工作?

python - 我在 Python 中的 Gstreamer 管道逻辑的基本问题。有什么建议吗?

bash - 如何将非交互式参数传递到使用 "read"的 bash 文件中?

regex - 删除两个方向的n行和sed中的匹配?

bash - 通过字符串初始化关联数组且无需声明

node.js - 指定 `env` 时,childProcess.spawn 失败