gpt4 book ai didi

Bash 构造 $(< 文件)

转载 作者:行者123 更新时间:2023-11-29 09:40:29 27 4
gpt4 key购买 nike

this answer我看到了下一个 Bash 结构。

yes "$(< file.txt)"

什么意思

"$(< file.txt)"  ?

我明白了

  • 命令替换 - $(command)用命令的结果替换。
  • 引号确保内容不会被格式化为一行,而是保持文件中的形式

虽然对上面的进行了实验,但接下来的命令也给出了相同的结果。

yes "$(cat file.txt)"

这个很清楚。 yes获取文件内容作为参数。

但不明白那些。

yes "$(< <(cat file.txt))
yes "$(< file.txt)"

特别是最后一个。 $(< file.txt) 中替换了什么命令??

最佳答案

  • < file基本上是在读取文件。

  • $(< file.txt)基本上是使用 $(...) 读取字符串中的文件内容构造

  • cat是一个外部命令,如果在 $(...) 中使用,它会导致 BASH 派生一个子 shell。

根据 man bash :

The command substitution $(cat file) can be replaced by the equivalent but faster $(< file).

关于Bash 构造 $(< 文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22823331/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com