BASH - echo 在 EOF 标签内如何工作

arc*_*col 0 bash

我想执行以下操作:

PASSWORD="mypassword"
RUNCOMMAND=$(cat <<EOF
echo $PASSWORD | sudo -S sudo echo "this is it babe"
EOF
)
Run Code Online (Sandbox Code Playgroud)

this is it babe我得到的不是 ,而是以下结果:

mypassword | sudo -S sudo echo "this is it babe"
Run Code Online (Sandbox Code Playgroud)

我尝试过cat <<\EOFcat <<'EOF'仍然没有运气。

有任何想法吗?

Att*_*tie 5

您将 aheredoc与 a混淆了pipeline

带有变量扩展的heredoc:

cat <<EOF
some text, possibly with variables: ${HOME} / $(whoami)
EOF
Run Code Online (Sandbox Code Playgroud)
some text, possibly with variables: /home/attie / attie
Run Code Online (Sandbox Code Playgroud)

没有变量扩展的heredoc:

some text, possibly with variables: /home/attie / attie
Run Code Online (Sandbox Code Playgroud)
some text, possibly with variables: ${HOME} / $(whoami)
Run Code Online (Sandbox Code Playgroud)

具有变量扩展的管道(注意引号,"):

cat <<"EOF"
some text, possibly with variables: ${HOME} / $(whoami)
EOF
Run Code Online (Sandbox Code Playgroud)
some text, possibly with variables: /home/attie / attie
Run Code Online (Sandbox Code Playgroud)

没有变量扩展的管道(注意引号,'):

some text, possibly with variables: ${HOME} / $(whoami)
Run Code Online (Sandbox Code Playgroud)
some text, possibly with variables: ${HOME} / $(whoami)
Run Code Online (Sandbox Code Playgroud)

${...}扩展环境变量

$(...)运行一个命令,并替换它的stdout


看起来您还试图输入密码sudo- 这不起作用,因为 sudo 将重新启动终端以获取您的密码,然后再将其传递stdin给最终应用程序。