Search code examples
linuxhostname

HOSTNAME missing from env output


I've got this C code getenv("HOSTNAME");, which returns NULL on a VM running Linux (Guixsd). While investigating I ran the following three commands, which work as expected:

hostname => guixos

echo $HOSTNAME => guixos

cat /etc/hostname => guixos (no newline)

So I expected hostname to also be printed out by env, but...

env | fgrep HOST =>

So, I've got three methods of accessing the hostname which actually work, and two which I expect to work, but don't. Can someone explain why those two methods don't also give me the hostname?


Solution

  • HOSTNAME is a variable that bash (but not other shells) sets by default. It is not exported.

    Since it's not exported, it will not be inherited by env or any C program invoked from a bash shell.