Unsetting environment variable with an empty name

Anders Lundstedt

For some reason, my system sets an environment variable with no name. This can be seen in the output from printenv as a line containing only = and causes problems for Python (more specifically the os.environ object).

How do I unset this environment variable? Is there any other workaround (e.g. prevent the variable to be set in the first place)?

To reproduce do e.g. env -i printenv, or for the Python problem do env -i = python -c "import os; os.environ.clear()"

Gilles 'SO- stop being evil'

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Unsetting an exported environment variable

Unsetting an environment variable vs. setting it to zero

Most defensive/robust way of unsetting an environment variable inside a shell script

Unsetting a variable in Velocity

Unsetting Superglobal Variable Properly

Environment variable empty on echo

Triggering an error on unsetting a traced variable

Environment variables and variable name

MissingEndParenthesisInMethodCall - checking if an environment variable is empty

Ansible variable name `environment` is reserved?

Postman get the environment name as a variable

How to fix session not starting after unsetting a variable

How to change the name of an environment variable with a different variable?

Variable substitution in Unix environment variable name

Why is my GID environment variable empty?

Is unsetting environment variables always cause some fallback behavior?

Unsetting Facebook Open Graph tags by giving them empty values?

Checking if a sessions is empty, unsetting it, and then sending $_POST data onclick

Intellij Error: Environment variable name is not set

Remove a folder with the same name as an environment variable

How to generate environment variable name dynamically and export it

cmd - Accessing Folder with Environment Variable Name

Using environment variable for volume name in docker compose

Renaming environment variables by changing variable name prefix

How to pass the name of environment variable as an argument?

Repository Name as a GitHub Action environment variable?

Does bash have a way to un-export a variable without unsetting it?

Is unsetting a single bit in flags safe with Python variable-length integers?

How to reference the value of an environment variable with using another environment variable value in variable name?