我正在尝试在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.