Grant Grant - 5 months ago 13
Linux Question

Passing multiple variables from local bash to remote bash script without gobbling

I'm having trouble sending multiple variables to a remote bash script without gobbling occurring.

For the sake of this question the variable $timestamp contains

12-12-15 19:45:21


ssh user@serverip "/usr/path/to/script.sh http://www.web.com/$1 http://web.com/$2 $timestamp";


I am sending 3 variables to script.sh

Two URLs with an amended file name in the form of a variable on the end and then my $timestamp variable

But on myscript.sh, when I try to insert $timestamp into a mysql database it only see's the first part of the date before the white space :

12-12-15


So my quotes around the command aren't preventing gobbling. Do I need to quote each variable separately?

Answer
ssh user@serverip "/usr/path/to/script.sh http://www.web.com/$1 http://web.com/$2 $timestamp";

This is equivalent to this locally calling

/usr/path/to/script.sh http://www.web.com/$1 http://web.com/$2 $timestamp

Try to quote each individual argument passed

ssh user@serverip "/usr/path/to/script.sh 'http://www.web.com/$1' 'http://web.com/$2' '$timestamp'";

You can also print each argument in the script to see what's being passed... e.g. echo $1, etc.