I have this code :
HOSTS="host1 host2"
For hostname in ${HOSTS} ;
do ssh -tt ${USERNAME}@${hostname} << EOF
HOSTSN="test"
echo ${HOSTSN}
exit
EOF
Done
The variable HOSTSN is empty, can you tell me why?
I have this code :
HOSTS="host1 host2"
For hostname in ${HOSTS} ;
do ssh -tt ${USERNAME}@${hostname} << EOF
HOSTSN="test"
echo ${HOSTSN}
exit
EOF
Done
The variable HOSTSN is empty, can you tell me why?
HOSTSN is empty because it is being evaluated at the time of the
do ssh -tt ${USERNAME}@${hostname} << EOF
execution. Using echo \${HOSTSN} instead will fix it.
Consider this example. Here a file 'foo' is created. The contents are not executed at the time of file creation. So $NAME is not set to anything.
$ cat >foo <<EOF
NAME=Steve
echo Hello ${NAME}
EOF
$
$ cat foo
Hello
$
By introducing a \ character, the variable is not evaluated at the time of file creation.
$ cat >foo <<EOF
NAME=Steve
echo Hello \${NAME}
EOF
$
$ cat foo
Hello Steve
$
man bash, if your shell is Bash.