0

When using a bash for loop and ssh'ing to multiple machines the hostname variable is not updated to be the name of the machine I've ssh'd into.

What am I missing here?

"A", "B", "C" are replaced with names of actual machines and the speech marks removed in the actual execution.

for node in "A" "B" "C" ;
do
    ssh $node "echo $node $HOSTNAME is alive"  ;
done

In the example above $node is correctly outputted but $HOSTNAME is the name of the machine which I SSH from.

1 Answer 1

2

Use single-quotes:

for node in "A" "B" "C" ;
do
    ssh $node 'echo $HOSTNAME is alive';
done

If you need to pass $node,

ssh [email protected] 'echo ' $node ' $HOSTNAME is alive'

If you use double-quotes, then you are passing the current machine's HOSTNAME value to a remote machine.

Sign up to request clarification or add additional context in comments.

1 Comment

@IanWatson, If this answer helped to solve your problem, Click the tick (check mark) on left under the vote arrows to accept the answer (meta.stackoverflow.com/questions/251078/…)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.