5

Is there any practical difference between unsetting an environment variable and setting it to the empty string?

5

Each application is free to interpret an unset variable and an empty variable in the same way or not. It is generally a bad idea to give them different meanings, and most applications don't do it, but it happens.

An example in shells themselves is the IFS shell variable (which is usually not exported in the environment, but the same principle applies). If unset, the shell behaves as if the value was $' \t\n'.

In a shell script, $foo expands to an empty string whether foo is set to an empty value or unset. You can run the shell under the nounset setting (set -u or set -o nounset), in which case the shell reports an error and exits if you try to expand an unset variable. Otherwise, you can distinguish between unset and empty variables with parameter expansion modifiers: ${foo:+a} expands to a if foo is unset or empty and to the empty string otherwise, whereas ${foo+a} expands to a even if foo is set to the empty string.

  • Where is the usage such as ${foo+a} documented? @Gilles – Jingguo Yao Jun 3 at 11:48
  • @JingguoYao In the bash manual under shell parameter expansion, for example. Or in the POSIX standard, for which there is a link in my answer. – Gilles Jun 3 at 16:49
0

There is a difference, yes. But it is very subtle since evaluating an empty string and an unset variable are the same.

$ env - FOO= sh -c 'echo "X${FOO}X X${FOO:+x}X X${FOO+x}X"; env'
XX XX XxX
FOO=
PWD=/tmp
$ env - FOO= sh -c 'unset FOO; echo "X${FOO}X X${FOO:+x}X X${FOO+x}X"; env'
XX XX XX
PWD=/tmp

Both will have ${FOO:+x} evaluate to an empty string. But ${FOO+x} will evaluate to a "x" when FOO is an empty string.

One thing that I commonly use this for is to set up a $DEBUG environment variable, and then use ${DEBUG+true} in the program. Then I can set DEBUG to anything, including an empty string.

In higher level languages, like Perl and Python, this is the difference between "" and undef (Perl) or None (Python).

  • That's specific to Bourne-derived shells, and the behavior can be changed (at least in bash) with set -o nounset. csh` and tcsh treat unset and empty variables quite differently. There's a very real difference in what's stored, and in what the C function getenv() will return. – Keith Thompson Dec 26 '11 at 22:47

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.