I want the hostname of a remote serve to use in my script.
I am trying this command in script but it is not working.
host= $(ssh -i keylocation ec2-user@ip sudo echo $HOSTNAME)
There are two issues with you code:
host=is significant, so you should remove it.
$HOSTNAMEgets expanded by the local bash.
host="$(ssh -i keylocation ec2-user@ip echo '$HOSTNAME')"
host="$(ssh -i keylocation ec2-user@ip hostname)"
I remove the
sudo, usually it is not needed to retrieve the hostname.