Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing variables from python to bash shell script via os.system

Tags:

python

bash

In the following code, I construct a variable $probe1 that I want to then pass to a bash script. I the toy example below, the output is blank, i.e. $probe1 is not recognized the the bash shell script within the os.system call. What needs to be done?

for line1 in datfile:
    datmat=datmat+[line1.rstrip('\n').split('\t')]
        probe=datmat[i][0]
        snp1=datmat[i][2]
    probe1='permprobes'+probe+'pheno.pphe'
    os.system('echo $probe1')
like image 261
user1815498 Avatar asked Dec 19 '12 01:12

user1815498


2 Answers

Seems like this is what you are trying to do:

In [2]: os.environ['probe1'] = 'hello'

In [3]: os.system('echo $probe1')
hello

But I have no idea why you would like to do this ...

like image 85
satoru Avatar answered Oct 06 '22 12:10

satoru


os.system('echo {0}'.format(probe1))

probe1 is a python variable, not a shell variable.

os.environ['probe1'] = probe1

will set the bash environment variable to the python variable contents. Once the python script exits though, the environment variable goes away.

like image 42
jgritty Avatar answered Oct 06 '22 13:10

jgritty