Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a script with a temporary environment variable?

Tags:

python

shell

If I have a python scripts with simple lines as the following:

import os

name = os.environ.get('BEAR_NAME')
if name != "":
  print("hello, ", name)

I'd like to set a temporary environment variable when running this script. Note that if I do

export BEAR_NAME="sleepybear"
python hello.py

the env var BEAR_NAME will still have value sleepybear once the python program finishes, which is not desired. Take docker's example, we can do docker run -e SOME_VAR=SOME_VAL to set the environment variable SOME_VAR. Is there a similar way when running a python script?

like image 922
IsaIkari Avatar asked May 22 '26 23:05

IsaIkari


1 Answers

Your shell command can be

BEAR_NAME="sleepybear" python hello.py
like image 151
ogdenkev Avatar answered May 24 '26 13:05

ogdenkev