Setting a local variable then running a command does not provide the variable to the child process:
$ GREETING=hi
$ python3 -c 'import os; print(os.environ.get("GREETING", "unknown"))'
unknown
However, setting a local variable in the same statement as the command does:
$ GREETING=hi python3 -c 'import os; print(os.environ.get("GREETING", "unknown"))'
hi
Exporting the variable is guaranteed to work, either with export
$ export GREETING=hi
$ python3 -c 'import os; print(os.environ.get("GREETING", "unknown"))'
hi
...or with set -o allexport
.
$ set -o allexport
$ GREETING=hi
$ python3 -c 'import os; print(os.environ.get("GREETING", "unknown"))'
hi