Skip to content
Advertisement

Preventing expansion/evaluation of parameter in ssh command

I have a shell script that runs a docker container on a remote server.

I’m trying to send the hostname of the remote server into the container but i just get the hostname of my local computer where i run the script.

The command looks like this in the script:

ssh $remote "docker run -h '`hostname`' 
                        -e 'VARIABLE=$SCRIPT_VAR' 
                        -e 'HOST_HOSTNAME=`hostname`' 
                        ..."

Both hostname and the environment variable host.hostname becomes the name of my local computer.

I know I can use singlequotes like this:

ssh $remote 'echo "`hostname`"'

and it will work. But then i cannot use scriptvariables like the $SCRIPT_VAR

How can i get it to evaluate on the remote server instead while also being able to use variables?

Advertisement

Answer

You still need to ensure that the expansion of $SCRIPT_VAR is quoted to prevent it from being subjected to word splitting or pathname expansion.

ssh $remote 'docker run -h "$(hostname)" 
                        -e "VARIABLE='"$SCRIPT_VAR"'" 
                        -e "HOST_HOSTNAME=$(hostname)" 
                        ...'
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement