Why is hostname and $HOSTNAME different?

Bo Berglund bo.berglund at gmail.com
Wed May 11 16:30:06 UTC 2022


This is in Ubuntu Server 20.04.4 LTS

I have some scripts that use the hostname to switch some paths when running.
It has been OK for a long time but now after I did a router f/w upgrade my
scripts fail.

And I think this is the cause of it all:

$ hostname
ubuntusrv
$ echo $HOSTNAME
ubuntuserv

Notice the difference in the return from these commands, which I thouight would
be identical.

Inside the scripts I use this construct:

if [ "$HOSTNAME" == "ubuntuserv" ]; then
  URLFILE="$HOME/www/url_strm.txt"   #Main web server file path
else
  URLFILE="$HOME/lan/url_strm.txt"   #Secondary device file path
fi

And it has worked fine for ages, but failed today after the router update.

Can the DHCP server in the router affect the hostname of a device which is using
it to get its IP address?
I had to go over and re-enter the DHCP reservations after a f/w update on the
router forced me to reset to factory and re-enter the complete config by hand.

If so what is the correct way in a script to get the *real* hostname which is
shown on the command line in terminal?


-- 
Bo Berglund
Developer in Sweden





More information about the ubuntu-users mailing list