Can (How do) you unset an environment variable on execution?

1

Many people are familiar with this method of setting env vars:

PYTHONPATH=/path/to/lib python do-a-thing.py

But I want to ensure the variable is unset before calling my command. Is it possible to do this in a one-line invocation, or do I have to do it this way?

unset PYTHONPATH; python do-a-thing.py

Wayne Werner

Posted 2017-06-13T20:17:06.143

Reputation: 1 501

Answers

3

You could use env for unsetting (or setting) environment variables: "The env utility executes another utility after modifying the environment as specified on the command line."

env -u PYTHONPATH python do-a-thing.py

Best thing is: this won't modify your current execution environment.

(Edit: apparently -u is a FreeBSD extension... please let us know if this works under Linux, too!)

jvb

Posted 2017-06-13T20:17:06.143

Reputation: 1 697

Looks like this works for me! – Wayne Werner – 2017-06-13T20:30:38.483

1+1. I confirm it works under Debian GNU/Linux 8 and Ubuntu 16.04.2 LTS. – Kamil Maciorowski – 2017-06-14T19:05:25.033