ssh and environment variables remote and local

bash, ssh

Solution

What should be expanded locally, keep the sigil `$` as is, like `$foobar`

What you want to be expanded remotely, you may use backslashes : `\$foobar`

By default in here-docs, the variable are expanded.

Ex. :

cat<< EOF
$UID
EOF

To avoid expanding in here-doc, you can use this form :

cat<< 'EOF'
$variable_that_will_not_been_expanded
EOF

or yours :

cat<< \EOF
$variable_that_will_not_been_expanded
EOF

both works.

Problem

I need to use a remote variable and a local variable in the same remote ssh command ``` export CASSANDRA_DIR=/opt/cassandra ssh root@sdi-prod-02 <<\EOF export READ=$(grep IPADDR /etc/sysconfig/network-scripts/ifcfg-eth0 |awk -F= '{print $2}') echo "listen_address: $READ" to directory "$CASSANDRA_DIR" EOF ``` The $READ variable is working just fine while the CASSANDRA_DIR is not working. The following does work for CASSANDRA_DIR ``` ssh root@sdi-prod-02 echo "directory=$CASSANDRA_DIR" ``` thanks, Dean

Original source