'How to pass parameters with quotes via ssh
When I run a script in my Linux server as follow:
./myscript \"hello\"
The script then receives the parameter as "hello". Now, I want to be able to run this script remotely from a different host via ssh. If the ssh connection was Linux to Linux, the following works:
ssh remote-host ./myscript \\\"hello\\\"
However, if you ssh connection was Windows 10 to Linux. The above does not work, the remote script receives the parameter as \hello"--note the extra backslash and missing double quote. I have tried the following and none works:
ssh remote-host ./myscript \\\"hello\\\"
ssh remote-host ./myscript ^"hello^"
ssh remote-host ./myscript ""hello""
ssh remote-host ./myscript '"hello"'
The only workaround I can think of is to create another shell script which contains:
./myscript \"hello\"
Then scp this script to the remote Linux server and execute it there. So, is there a way for me to properly quote my arguments?
Solution 1:[1]
You can use following convenient wrapper script ssh.sh
cat <<'EOF' > ssh.sh
#!/bin/bash
function escape() {
for arg in "$@"; do
printf "%q " "$arg"
done
}
ssh $(escape "$@")
EOF
chmod +x ssh.sh
Then you can safely call ssh via the ssh.sh, without worrying about escaping.
e.g.,
./ssh.sh host sh -c 'ls /'
Compare to standard ssh,
ssh host sh -c 'ls /'
does not work, it need be converted to
ssh host sh -c \'ls /\'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
