Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

ssh user@123.456.789.012 'foo'


ssh user@123.456.789.012 "foo"

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

share|improve this question
In what way do the results differ? – ColoradoEric Mar 10 '12 at 15:25
up vote 8 down vote accepted

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
$ 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"
share|improve this answer
I am not passing any variables, and I am getting different results when running a command remotely. – BassKozz Mar 10 '12 at 15:12
It may help if you include the exact command you are putting inside the quotes in your question. – Ladadadada Mar 10 '12 at 15:25
@BassKozz: besides things with $, escapes (backslashes) are also treated differently between single- and double-quotes. Are there any of those in your remote command? – Gordon Davisson Mar 10 '12 at 15:46
@Ladadadada Please consider this edit suggestion: I think it was too much for me to approve and should be been left as a comment, but may be accurate. – Chris S Mar 22 '12 at 12:40
I tested these as I posted each one and I just tested the subshell ones again to make sure, using GNU bash, version 3.2.48. Your results may differ if you use a different shell or possibly even a different version of bash. – Ladadadada Mar 22 '12 at 12:48

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.