Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

I'm trying to execute a remote script with SSH, and this script changes its behavior depending on whether or not a specific argument is empty. Here's a minimal example, where I created a simple file that echos the number of arguments and then what the arguments are. When I execute it locally, it works as expected, saying it got 3 arguments. But when I execute the script through SSH, it says it only got 2 arguments:

$ cat 

echo $#
echo $@
$ ./ foo '' bar
foo bar
$ ssh user@localhost ./ foo '' bar
foo bar

Is there a way to get SSH to forward the empty argument, so the script says it got 3 arguments?

share|improve this question
up vote 2 down vote accepted


  ssh user@localhost "./ foo '' bar"
share|improve this answer
Thanks, that's great! – Cornstalks Jan 9 '14 at 23:00

I do something similar with my remote Perl invocations that should work for your bash script as well - with some bash syntax changes, of course...

Use environment variables to "pass variables" (set or otherwise) to the remote process. For instance,

$ ssh me@localhost 'VAR1="var1 text" /tmp/'

then, in your remote script, just check for variable existence, as in:

$ cat /tmp/
if ($ENV{VAR1}) { print "$ENV{VAR1}\n"; }
if ($ENV{VAR2}) { print "$ENV{VAR2}\n"; }

so when the above is invoked

$ ssh me@localhost 'VAR1="var1 text" /tmp/'
var1 text

As you can see, I was able to check for the input variable existence on the remote call.

This should work for you.

share|improve this answer
Interesting. Neat idea. – Cornstalks Jan 10 '14 at 0:04

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.