使用heredoc或其他技术创建单个字符串参数

Ale*_*lls 2 shell bash cat here-document

我正在尝试在远程服务器上执行脚本,并将脚本作为最后一个参数传递

ntrs exec-all-ubuntu --exec `cat << 'EOF'

  echo "$(pwd)"
  echo "$foobar"

EOF`
Run Code Online (Sandbox Code Playgroud)

问题是文本中的值作为单独的参数发送,echo 是第一个参数,pwd 值是第二个单独的参数,但我只想要一个参数作为字符串

参数最终看起来像这样:

[ '--exec', 'echo', '"$(pwd)"', 'echo', '"$foobar"' ]
Run Code Online (Sandbox Code Playgroud)

但我正在寻找带有换行符的字面内容:

[ '--exec', '   echo "$(pwd)"\n\n echo "$foobar"\n ' ]
Run Code Online (Sandbox Code Playgroud)

我也尝试使用这个:

ntrs exec-all-ubuntu --exec `read -d << EOF
    select c1, c2 from foo
    where c1='something'
EOF`
Run Code Online (Sandbox Code Playgroud)

但该字符串是空的

che*_*ner 6

您可以简单地使用带有嵌入换行符的常规字符串:

ntrs exec-all-ubuntu --exec '
  echo "$(pwd)"
  echo "$foobar"
'
Run Code Online (Sandbox Code Playgroud)