1

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?

0

1 Answer 1

2

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']}")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.