Home » Questions » Computers [ Ask a new question ]

Existence of USER environment variable in the shell

Existence of USER environment variable in the shell

I heard (although I can't find any sources for proof) that the USER environment variable may not be set in a old Unix shells (maybe even some obscure shells as well). What is the probability that it won't be set?

Asked by: Guest | Views: 324
Total answers/comments: 1
Guest [Entry]

"The probability is very low.

if you want a fallback when writing a script:

USER=${USER:-`whoami`}

...will default $USER if it's unset.

For really old pre-POSIX Bourne shells, you'd want:

test -z ""$USER"" && USER=`whoami`"