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

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!)

| improve this answer | |
  • Looks like this works for me! – Wayne Werner Jun 13 '17 at 20:30
  • 1
    +1. I confirm it works under Debian GNU/Linux 8 and Ubuntu 16.04.2 LTS. – Kamil Maciorowski Jun 14 '17 at 19:05

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.