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 tmp.sh 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 argtest.sh 

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

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



  ssh user@localhost "./argtest.sh foo '' bar"

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/try.pl'

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

$ cat /tmp/try.pl
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/try.pl'
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.

  • Interesting. Neat idea. – Cornstalks Jan 10 '14 at 0:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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