How do I get xinit to see environment variables?

Given the following xinitrc:

echo test $HOSTNAME test

I see the output test test on the terminal, which indicates $HOSTNAME is not set when xinit runs, even though echo $HOSTNAME on the terminal (that runs startx), it works as expected

1 Answer 1


$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.

  • Thank you for your in-depth answer, it's really useful! I knew there was a difference between un/exported variables, but I had no idea that the shell created some of them by default, or how to check it. I also didn't know xinitrc can be any executable. Great stuff! Apr 26, 2020 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.