Home > other >  bash: ssh : run command on remote machine with expanding variables
bash: ssh : run command on remote machine with expanding variables

Time:10-10

I am trying the following

act_url="https://someurl"
url=`ssh [email protected] 'somecommand "$act_url"'`
echo $url

Here I want $act_url to be replaced with https://someurl

url=`ssh [email protected] 'somecommand "https://someurl"'`

But its not working.

CodePudding user response:

You need to use double quotes, not single quotes, if you want an expansion to take place. Double quotes inside a single-quoted string are still inside a single-quoted string -- they're just literal text, not (local) shell syntax.

To do this safely, in a way that doesn't let hostile URLs run arbitrary commands, use printf '%q ' to generate the command you're going to run:

act_url="https://someurl"
printf -v rmt_cmd '%q ' somecommand "$act_url"
url=$(ssh [email protected] "$rmt_cmd")
echo "$url"
  •  Tags:  
  • bash
  • Related