How to use shell environment variables in python?


I am new to Python programming and i want to use shell environment variables in my programs. And also i want to be able to run Linux commands from my python programs.
How can i import shell variables and Linux commands in python programs.

And also, i want to load my programs into the python shell to run them. If any one also has ideas concerning best practices to learn python programming, your advice is welcome please.

This is relatively easy to do.

Take a look at the following example:

Basically, you have to begin your Python program by importing the os module (which allows you to access the operating system’s commands), and then invoke it as follows:

os.system("Linux command inside double quotes here")

As you can see, by using the os module you can access shell environment variables the same way you would from a bash shell script.

Give the Python program execute permissions and run it:

chmod +x


And here’s the result:

As for best practices, I would highly recommend you look for a Python course in or They have several free and high quality courses for all levels of experience.

Hope it helps!

1 Like

Yes it has worked well for me, thank you.