Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HOSTNAME missing from env output

Tags:

linux

hostname

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?

like image 340
crinklywrappr Avatar asked Jun 21 '26 07:06

crinklywrappr


1 Answers

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.

like image 84
that other guy Avatar answered Jun 24 '26 00:06

that other guy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!