The $HOSTNAME Environment Variable

Humble beginnings

Not too long ago (or well, in the past week in fact), I was configuring a program that had to be configured through a yaml file. Since I wanted to create a configuration that could be used across multiple servers, I needed to somehow embed the hostname of the host machine to the config file. Luckily, the program in question had support for expanding ${ENV_VAR} into the contents of environment variable ENV_VAR. So off to the races! Or so I thought.

There’s nothing

After trying to specify variable like instance: ${HOSTNAME}, I was left wondering why the given entry was empty. After finding out that the program was using go package envsubst I tried to debug the problem directly with it, but it was no good. What ever I tried the hostname variable didn’t get expanded, even tho' echo $HOSTNAME gave the correct result.

The realization

So after embarrassingly many hours later, it struck me. It must be a bash builtin! Checking the output of env didn’t contain HOSTNAME. Ffs… Sure enough, the bash documentation says that it sets some variables, including the HOSTNAME. I knew that bash has some builtin commands, but didn’t knew about the variables.

So I guess I learned not to rely on echo to tell you the full story of environment variables, but check the output of env instead.