path="/var/lib/Fingerprint log.log"
ssh root@$server test -d $path
The short answer is that you should run ssh like this:
ssh root@$server test -d "'$path'"
or
ssh root@$server "test -d '$path'"
The double quotes ensure that the $path
variable is expanded on the local system, and also that the single quotes are passed through to the remote system. The single quotes ensure that the value of $path
is still treated as a single command-line argument on the remote system.
This is due to how ssh handles remote commands that are specified on its command line. First, it concatenates all of the relevant command-line arguments into a single space-separated string. Then it sends that string to the remote system, where it's run as a shell command.
For example:
$ dne='/var/does not exist'
$ ssh localhost ls $dne
ls: cannot access /var/does: No such file or directory
ls: cannot access not: No such file or directory
ls: cannot access exist: No such file or directory
$ ssh localhost ls "$dne"
ls: cannot access /var/does: No such file or directory
ls: cannot access not: No such file or directory
ls: cannot access exist: No such file or directory
$ ssh localhost ls "'$dne'"
ls: cannot access /var/does not exist: No such file or directory