3

I’m trying to run a script on a remote machine but I’m facing issue with the parameter that I’m passing to that script. I am passing a variable which is a string containing multiline as input.

The main script is named parent.sh which calls child.sh on remote machine. This child.sh is actually copied to the remote machine and then executed on that machine.

$ cat parent.sh
#!/bin/bash
/usr/bin/scp /home/tom/child.sh harry@remote_server:
printf "Key in parent script: $key\n\n"
/usr/bin/ssh -t harry@remote_server "sudo sh -c '/home/harry/child.sh '$key''"


$ cat child.sh
#!/bin/bash
printf "Key in child: $1"

Input:

key has the following content:
fruit=apple
vegetable=potato

Output:

Key in parent script: fruit=apple
vegetable=potato

Key in child: fruit=apple

Problem is that in the child script's output, i'm only getting the first line but not the second line (vegetable=potato). I tried few combinations of single and double quotes but nothing seems to be working. I would really love to know what blunder am i committing here and how can i rectify it.

4
  • Does this work: `"sudo sh -c '/home/harry/child.sh \"$key\"'"? Commented Apr 24, 2015 at 18:30
  • Thanks for the quick response @EtanReisner! :) Yes, it does work and i was just thinking how i missed giving that one a try. Thanks a lot! :) But now i'm confused why my syntax is not working. Why is it just passing one line instead of both? Or is it passing both but i'm not accepting it properly in child script? Commented Apr 24, 2015 at 18:44
  • Your syntax is leaving the expansion of $key unquoted in the arguments passed to sh. You probably have the second line available as $2 with that. The "inner" single quotes you probably thought were quoting the expansion of $key are actually just ending the single quoted string and then starting a new (empty) single quoted string at the end of the command. Commented Apr 24, 2015 at 18:46
  • (@Etan Reisner) That was great! in my case -t was not required. Commented Dec 5, 2023 at 14:27

1 Answer 1

4

This command should work:

/usr/bin/ssh -t harry@remote_server "sudo sh -c '/home/harry/child.sh \"$key\"'"

The key here is that you need the inner command to keep the value of $key quoted when it executes.

Your original command:

"sudo sh -c '/home/harry/child.sh '$key''"

which you probably thought was being read as:

"{sudo} {sh} {-c} {/home/harry/child.sh {$key}}"

is actually read as:

"{sudo} {sh} {-c} {/home/harry/child.sh }$key{}"

which leaves the expansion of $key unquoted and it then gets word-split and the newline becomes just another normal space. You probably have the second "line" available as $2 in the script.

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

2 Comments

Well, i was about to request you to post your comments as answer. I'm glad you already read my mind and i'm even more happy with the way you explained it in this answer. The only thing i'm feeling bad about is that i can upvote it only once. :( Anyways, thanks a ton! :) This consumed a significant amount of time today but i'm glad i can sleep happily now.
And yes, you were right about $2 too. It did contain the missing line. Thanks! :)

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.