Osama Dar
Osama Dar

Reputation: 454

Python subprocess export environment variables (which use other environment variable)

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

Answers (1)

Arkadiusz Drabczyk
Arkadiusz Drabczyk

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

Related Questions