Environment variables aren't supposed to have an empty name, so many utilities don't support them.
The env
command from GNU coreutils supports setting the environment variable with an empty name but not unsetting it. That's a bug.
$ env '=wibble' env |grep wibble
=wibble
$ env '=wibble' env -u '' env
env: cannot unset `': Invalid argument
Common shells can't unset the empty name either. That's ok, since the empty name isn't supposed to be used as an environment variable, and can't be used as a shell variable. Zsh is the only buggy one in the lot: it pretends to do the job but in fact does nothing.
$ env '=wibble' dash -c 'unset ""'
dash: 1: unset: : bad variable name
$ env '=wibble' bash -c 'unset ""'
bash: line 0: unset: `': not a valid identifier
$ env '=wibble' ksh -c 'unset ""'
ksh[1]: unset: : invalid variable name
$ env '=wibble' mksh -c 'unset ""'
mksh: : is read only
$ env '=wibble' posh -c 'unset ""'
posh: unset: is read only
$ env '=wibble' zsh -c 'unset ""'
$ env '=wibble' zsh -c 'unset ""; env' | grep wibble
=wibble
Python, as you've noticed, bugs out when it finds the empty name for an environment variable.
Perl has no such problem, so it may be a solution for you. Note that you have to execute a new shell to use an external process to change the environment.
perl -e 'delete $ENV{""}; exec $ARGV[0] @ARGV' "$SHELL" "-$-"
exec env -u '' "$SHELL"
will remove it. – Stéphane Chazelas Jan 10 '15 at 23:52env: cannot unset ‘’: Invalid argument
– Anders Lundstedt Jan 11 '15 at 0:05exec bash -c 'exec "$SHELL"'
sincebash
strips that. – Stéphane Chazelas Jan 11 '15 at 0:16posix.unsetenv
. POSIX doesn't mandate compliant OSes to support empty names, but Python should support them on OSes that do. – Gilles 'SO- stop being evil' Jan 11 '15 at 22:20env
from GNU coreutils as well, since it can set but not unset a variable with an empty name. – Gilles 'SO- stop being evil' Jan 11 '15 at 22:26