This question already has answers here:

is it possible to use variables in remote ssh command?
(2个答案)
我试图通过ssh在远程服务器上执行一些shell脚本。
下面给出了一个代码示例:
ssh -i $KEYFILE_PATH ubuntu@$TARGET_INSTANCE_IP "bash"  << EOF
#!/bin/bash
cat /home/ubuntu/temp.txt
string=$(cat /home/ubuntu/temp.txt )
echo $string
EOF

cat打印预期结果,但是
$string不打印任何内容。
如何将cat的返回值存储在变量中?

最佳答案

您需要将here doc的内容设置为文本,否则它们将在当前shell中展开,而不是在所需的远程shell中展开。
引用EOF

ssh .... <<'EOF'
...
...
EOF

08-18 13:42
查看更多