小编Jon*_*Jon的帖子

在bash heredoc中使用变量

我正在尝试在bash heredoc中插入变量:

var=$1
sudo tee "/path/to/outfile" > /dev/null << "EOF"
Some text that contains my $var
EOF
Run Code Online (Sandbox Code Playgroud)

这不符合我的预期(按$var字面意思处理,不进行扩展).

我需要使用,sudo tee因为创建文件需要sudo.做类似的事情:

sudo cat > /path/to/outfile <<EOT
my text...
EOT
Run Code Online (Sandbox Code Playgroud)

不起作用,因为>outfile在当前shell中打开文件,而不使用sudo.

variables bash heredoc sh

175
推荐指数
3
解决办法
10万
查看次数

标签 统计

bash ×1

heredoc ×1

sh ×1

variables ×1