0

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?

2
  • 1
    Probably the space in your timestamp is the problem. Add single quotes around $timestamp and see if that fixes it. Commented Jun 20, 2016 at 20:40
  • Nope, still only receiving the first part? Commented Jun 20, 2016 at 20:47

3 Answers 3

1
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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Peter, this what exactly what I needed. I tried quoting everything before but without mixing the single and double quotes. Thanks man!
The reason you need both sets of quotes is that your shell parses, applies, and then removes the outer level (the double-quotes), passes the result to ssh, which passes that to a remote shell, and that parses (and removes) the inner level (single-quotes in this example), and then the remote shell passes the result of that to script.sh. With only one level of quotes, it was unquoted by the time the remote shell parsed it, and hence got split.
Thanks for the detailed explanation Gordon!
0

You can try something like

ssh localhost "printf \"%s %s %s\n\" a b \"last parameter\""

Comments

0

You need to escape the values for the remote host. The correct way of doing this is with printf %q:

ssh user@serverip "/usr/path/to/script.sh \
    $(printf "%q " "http://www.web.com/$1" "http://web.com/$2" "$timestamp")"

This works for all variable values. Wrapping them in single quotes would instead result in syntax error and command injection when the variables themselves contain single quotes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.