0

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?

1
  • 3:1. no, wait... 17:1. no, wait... Jan 29, 2010 at 0:06

2 Answers 2

2

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`
0

Well, are you expecting to use any old shells, or are you expecting users of your program to do so?

Anyway, there's always id -un, though I have no idea whether this is more or less universal than $USER. You could have your script try both.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .