$HOSTNAME is not an environment variable until you export
it. Until then, it is only an internal "shell variable" only known to this one process you're typing the commands into. (There are a few other built-in shell variables like this, and any custom variables you set also behave this way.)
Your tests using echo
do not reveal the difference because all variables are expanded by the shell itself, not by the 'echo' command.
Instead, use env
or printenv
to see the actual environment block that's inherited by child processes. (Alternatively you could use declare -p HOSTNAME
to check whether it has the 'x' (export) flag.)
Note that the presence of $HOSTNAME doesn't actually depend on whether you're trying to use it interactively or through a script ‐ it appears depending on which shell is being used; for example Bash provides it but Sh or Dash do not.
So if your xinitrc script had the #!/bin/bash
header, it would be able to use $HOSTNAME even if it wasn't inherited through environment.