How to Pass Variables from Python Script to Bash Script

How to pass variables from python script to bash script

I would print it to a file chosen on the command line then I'd get that value in bash with something like cat.

So you'd go:

python tempfile.txt
var=`cat tempfile.txt`
rm tempfile.txt

[EDIT, another idea based on other answers]

Your other option is to format your output carefully so you can use bash functions like head/tail to pipe only the first/last lines into your next program.

Passing variable to bash script from Python

Firstly, in the bash file you are supposed to do:

clustername=$1 # invoking from pythonscript

to get the command line arguments passed into the script.

Secondly, using check_call(), you will not be able to catch the output from the bash script. You will need to use or subprocess.check_output() or something similar to achieve that.

You can try doing the following:

import sys
import subprocess

print(["/data/path/", str(clustername)], universal_newlines=True).stdout)

check_call() will only return the return code and not the stdout. You can also try check_output() if you want to see the output.

You can also do the following using check_output:

print(subprocess.check_output(["/data/path/", str(clustername)], universal_newlines=True))

Removed shell=True as suggested in the comments.

Pass variable from Python to Bash

  1. Unless Python is used to do some kind of operation on the original data, there's no need to import anything. The answer could be as lame as:

     myvar=$(python - <<< "print 'second'") ; echo "$myvar"
  2. Suppose for some reason Python is needed to spit out a bunch of bash variables and assignments, or (cautiously) compose code on-the-fly. An eval method:

    eval "$(python - <<< "print('myvar=second')" )"
    echo "$myvar"

Pass a variable from python to shell script

If call(['bash', '']) is working without arguments, there is no reason why it shouldn't work when additional arguments are passed.

You need to substitute the values of the variables into the command line arguments, not just pass the names of the variables as strings as does this:

call(['bash', '', 'var1', 'var2'])

Instead, do this:

var1 = '1'
var2 = '2'
call(['bash', '', var1, var2])

Now this will work providing that var1 and var2 are strings. If not, you need to convert them to strings:

var1 = 1
var2 = 2
call(['bash', '', str(var1), str(var2)])

Or you can use shlex.split():

cmd = 'bash {} {}'.format(var1, var2)

Call Python script from bash with argument

To execute a python script in a bash script you need to call the same command that you would within a terminal. For instance

> python var1 var2

To access these variables within python you will need

import sys
print(sys.argv[0]) # prints
print(sys.argv[1]) # prints var1
print(sys.argv[2]) # prints var2

How to use variables from shell script in python script file?

Just use command line arguments:

Shell Script

python "$a" "$b"

Python Script

import sys
var1 = sys.argv[1]
var2 = sys.argv[2]
print var1, var2

Related Topics

Leave a reply