0

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?

jww
  • 97,681
  • 90
  • 411
  • 885
mTv
  • 1,074
  • 17
  • 28
  • Why the downvote? – mTv Nov 14 '18 at 11:17
  • Possible duplicate of [Difference between single and double quotes in Bash](https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash) – Ipor Sircer Nov 14 '18 at 11:30
  • Not a duplicate. I know the difference. I just want something in between. Think i figured it out myself though. – mTv Nov 14 '18 at 11:45
  • Your "solution" leaves `$SCRIPT_VAR` unquoted, which may break your command if it contains whitespace. – chepner Nov 14 '18 at 15:26
  • Please place answers in Answer blocks. Later, you can accept your own Answer. Also see [How does accepting an answer work?](https://meta.stackexchange.com/q/5234/173448) – jww Nov 14 '18 at 22:54

1 Answers1

1

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)" \
                        ...'
chepner
  • 497,756
  • 71
  • 530
  • 681