Ssh – Difference between single quotes and double quotes when passing command through ssh

bashssh

When I send a command to a remote server over ssh what is the difference between:

ssh user@123.456.789.012 'foo'

and

ssh user@123.456.789.012 "foo"

I am not passing any variables, and I am getting different results when running a command remotely.

Best Answer

Probably no difference for that example but there certainly would be for this one:

ssh user@123.456.789.012 "echo $PATH"

The reason is that bash will evaluate and substitute variables inside double-quotes on the local machine but will do it on the target machine inside single quotes.

The same is true for subshells:

ssh user@123.456.789.012 "echo `hostname`"
ssh user@123.456.789.012 "echo $(hostname)"

However, it appears to not be true for functions:

$ foo () { echo "Foo"; }
$ foo
Foo
$ ssh user@123.456.789.012 "foo"
bash: foo: command not found

Globbing also does not happen within double quotes:

$ ssh user@123.456.789.012 "ls -l *"
$ ssh user@123.456.789.012 "ls -l numbered_files.?.gz"