Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 
#!/bin/bash

echo $#
echo $@
$ ./argtest.sh foo '' bar
3
foo bar
$ ssh user@localhost ./argtest.sh foo '' bar
2
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

2 Answers 2

up vote 2 down vote accepted

Try

  ssh user@localhost "./argtest.sh foo '' bar"
share|improve this answer
    
Thanks, that's great! –  Cornstalks Jan 9 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/try.pl'

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

$ cat /tmp/try.pl
#!/usr/bin/perl
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.

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

Your Answer

 
discard

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.