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.
$keyunquoted in the arguments passed tosh. You probably have the second line available as$2with that. The "inner" single quotes you probably thought were quoting the expansion of$keyare actually just ending the single quoted string and then starting a new (empty) single quoted string at the end of the command.-twas not required.