![如何避免将变量传递给 SSH 时出现“错误的转义字符”?](https://linux22.com/image/1186124/%E5%A6%82%E4%BD%95%E9%81%BF%E5%85%8D%E5%B0%86%E5%8F%98%E9%87%8F%E4%BC%A0%E9%80%92%E7%BB%99%20SSH%20%E6%97%B6%E5%87%BA%E7%8E%B0%E2%80%9C%E9%94%99%E8%AF%AF%E7%9A%84%E8%BD%AC%E4%B9%89%E5%AD%97%E7%AC%A6%E2%80%9D%EF%BC%9F.png)
我有一个从命令行传递的字符串变量,我想在 SSH 中使用它。
#!/bin/bash
while getopts ":b:e:" arg; do
case "${arg}" in
b) b="$OPTARG";;
e) e="$OPTARG";;
esac
done
echo "locally using $b and $e"
ssh @myhost "$b $e" << 'EOF'
echo "Remotely using $b and $e"
EOF
输出:
locally using 06-30-20-18-10 and 06-30-20-23-59
Bad escape character '06-30-20-23-59'
运行:bash file.sh -b 06-30-20-18-10 -e 06-30-20-23-59
如何避免错误的转义字符并远程使用变量?