Run a local script on a remote server using ssh with out having to worry about quotes

Solution 1:

With the following test script:

#!/bin/bash
uname -n
a=(b c 'd e' f)
echo "${#a[@]}"
b=(1 "2 3" 4)
echo "${#b[@]}"

The output should be the remote host name followed by 4 and 3 when executed using:

ssh user@server "$(<scriptname)"

This shows that the quotes inside the script are being handled correctly. If the quotes weren't being handled correctly, the numbers output would be 5 and 4 due to word splitting. Note the quotes around the command substitution.

By the way, the first command you posted doesn't work. The single quotes prevent the backticks from being evaluated locally.

Solution 2:

cat local-script.sh | ssh user@server bash

or whatever shell you want to use instead of bash