【问题标题】:tee command wont work with file name in a variabletee 命令不能在变量中使用文件名
【发布时间】:2022-11-27 13:01:33
【问题描述】:

我试图将 bash 脚本的内容输出到文件中,但是当我将文件名放入变量时,它不起作用。但是如果我硬编码相同的文件名,它就可以工作。

我试过这个

{
echo "in the script"
file='file.txt'
} | tee -a "$file"

但是我收到错误 tee: : No such file or directory 我也回显“$file”并返回 file.txt,所以我知道变量设置正确。 当我做:

{
echo "in the script"
} | tee -a "file.txt"

它创建文件并填充它没问题。为什么我的变量在这里不起作用?

【问题讨论】:

    标签: bash tee


    【解决方案1】:

    我相信,当您使用方括号对命令进行分组时,它会创建一个上下文,如果您在其中定义变量,它们将只存在于该上下文中,因此不会在管道之后传递给命令。

    你可能想做这样的事情吗?

    file="file.txt" && 
    {
      echo "in the script"
    } | tee -a "$file"
    

    【讨论】:

      猜你喜欢
      • 2016-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多