7

I am trying to write a script which is executing couple of things on a Linux server and I would like to use bash for most of the Linux specific commands and only use Python for the most complex stuff, but in order to do that I will need to export some variables from the bash script and use them in the python script and I didn't find a way how I can do that. So I have tried to create two very little scripts to test this functionality: 1.sh is a bash script

#!/bin/bash

test_var="Test Variable"
export test_var
echo "1.sh has been executed"
python 2.sh

2.sh is a Python script:

#!/usr/bin/env python3

print("The python script has been invoked successfully")
print(test_var)

As you can guess when I execute the first script the second fails with the error about unknown variable:

$ ./1.sh
1.sh has been executed
The python script has been invoked successfully
Traceback (most recent call last):
  File "2.sh", line 4, in <module>
    print(test_var)
NameError: name 'test_var' is not defined

The reason why I am trying to do that is because I am more comfortable with bash and I want to use $1, $2 variables in bash. Is this also possible in Python?

[EDIT] - I have just found out how I can use $1 and $2 it in Python. You need to use sys.argv[1] and sys.argv[2] and import the sys module import sys

3
  • 2
    Why not just use os.environ['test_var'] if you wan't to access environment variables? Commented May 29, 2018 at 8:23
  • stackoverflow.com/questions/4906977/… Commented May 29, 2018 at 8:24
  • 1
    You also could pass variable to Python scripts: python 1.py "variable-value" Commented May 29, 2018 at 8:25

2 Answers 2

8

To use environment variables from your python script you need to call:

import os
os.environ['test_var']

os.environ is a dictionary with all the environment variables, you can use all the method a dict has. For instance, you could write :

os.environ.get('test_var', 'default_value')
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks it works, but I think you can easily omit the , 'default_value' part. It works just fine with os.environ.get('test_var')
It will also work if you use a simple dict lookup os.environ['test_var']. Use the get method with a default only when it make sense - when the value will not always exist in the environment variable.
7

Check python extension it should be .py instead of .sh 1.sh

#!/bin/bash
 test_var="Test Variable"
 export test_var
 echo "1.sh has been executed"
 python 2.py

os library will gave you the access the environment variable. Following python code will gave you the required result,

#!/usr/bin/env python3
import os
print("The python script has been invoked successfully")
print(os.environ['test_var'])

Check for reference : How do I access environment variables from Python?

6 Comments

I think here you need to put .get in your print statement, without it it doesn't work: print(os.environ.get['test_var'])
@GeorgеStoyanov I have executed above code on ubuntu and It worked without .get.
if I execute it without the .get I get an error: AttributeError: _Environ instance has no __call__ method. I am also using Ubuntu 16.04.4 LTS.
If i execute with .get , get an error Traceback (most recent call last): File "2.py", line 4, in <module> print(os.environ.get['test_var']) TypeError: 'method' object is not subscriptable
ohh i got the mistake .get is the method so os.environ.get('test_var') works on my end and there is also alternative os.environ['test_var']
|

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.