Reputation: 454
I want to replicate the following bash export command in Python:
export my_shell=$SHELL
If I execute the above command in bash and echo after that:
echo $my_shell
/bin/bash
How do this in Python? I know we can use export command (which does not use environment variable) using os.environ()
like following:
import os
os.environ['my_shell'] = "my_shell_string"
but how to pass another environment variable (in this case: $SHELL
) as an input when exporting like above?
Upvotes: 1
Views: 678
Reputation: 12373
You can use a value from os.environ
dictionary as rvalue as well:
#!/usr/bin/env python3
import os
print(f"SHELL: {os.environ['SHELL']}")
os.environ['my_shell'] = os.environ['SHELL']
print(f"my_shell: {os.environ['my_shell']}")
Upvotes: 2