I have a command in my bash_profile such as id=12345 which I defined the following alias
alias obs="echo $id" since the id will chance over time.
Now what I want to do is call this alias in my python script for different purposes. My default shell is bash so I have tried the following based on the suggestions on the web
import subprocess
subprocess.call('obs', shell=True, executable='/bin/bash')
subprocess.call(['/bin/bash', '-i', '-c', obs])
subprocess.Popen('obs', shell=True,executable='/bin/bash')
subprocess.Popen(['/bin/bash', '-c','-i', obs])
However, none of them seems to work! What am I doing wrong!
1 Answer 1
.bash_profile is not read by Popen and friends.
Environment variables are available for your script, though (via os.environ).
You can use export in your Bash shell to export a value as an environment variable, or use env:
export MY_SPECIAL_VALUE=12345
python -c "import os; print(os.environ['MY_SPECIAL_VALUE'])"
# or
env MY_SPECIAL_VALUE=12345 python -c "import os; print(os.environ['MY_SPECIAL_VALUE'])"
2 Comments
env like this MY_SPECIAL_VALUE=18 python ...env is portable.
id), only.