How to pass variables from python script to bash script

pass variable to python script
return value from python script to batch file
return string from python to shell script
python script return value to command line
call python function from bash script
python' return array to shell script
python return value to shell
pipe python output to bash

I have a bash script, , and in it I have call a python script . The python script calculates something, and I want it to return a value that will be used later in . I know I can do




print x # when x is the value I want to pass

But this is not so convenient, because I also print other messages in

Is there any better way to do it?


What I'm doing now is just

var=`python | tail -n 1`

It means I can print many things inside, but only the last line (the last print command, assuming it doesn't contain "\n" in it) will be stored in var.

Thanks for all the answers!

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.

piping python variable value to bash script (inside python script , Python doesn't expand variables in strings in the same way as bash. If you have VAR in python and want to pass that to bash you could do So, a solution to your problem, is to pass your Bash args as a string even with spaces, like this example: $> export var1="hello, hi, hola" $> python "$var1" ['', 'hello, hi, hola'] For more informations see this article

I believe the answer is


import sys 
b = int(sys.argv[1]) 
###your python script can still print to stderr if it likes to 
print >> sys.stderr, "I am no converting" 
result = a[b] 
print result



text=`python $num` 
echo "$num as text is $text" 

command line - Pass a bash variable to python script, The python equivalent of the shell's positional parameter array $1 , $2 etc. is sys.​argv. So: #!/usr/bin/env python import sys def getPermutation(s  It depends on the encoding used in the bash script file. It should match the one defined in your locale. Issue the "locale" command. It will list several environment variables (the important one is LANG).

I'm not sure about "better", but you could write the result to a file then read it back in in Bash and delete it afterwards.

This is definitely ugly, but it's something to keep in mind in case nothing else does the trick.

[Linux/Python] Pass variable from python to bash script, Anyone familiar with this?['./'], shell=True). How would I go about passing a python variable to that  You can easily pass command line arguments to a Python script. In this tutorial, we will help you to read the command line arguments in a Python script. Below is the sample Python script, which reads the command line arguments and print details. Create a sample script like and copy below content.

In your python script, redirect another messages to stderr, and print x to stdout:

import sys
print >>sys.stderr, "another message"
print x

in the bash script:

var=`python 2>/dev/null`

Also, if x is an integer between 0,255, you can use exit code to pass it to the bash:

import sys

in bash:


Please note that exit code is used to indicates errors, 0 means no error, and this breaks the convention.

How to call Python script from bash with an agrument?, I want to pass an argument in my python script from my bash script, how can I do that? And to access these arguments in python you can use: Example of Passing Arguments in a Bash Script If you developed a script called that counts the words in a file, it's best to pass the file name as an argument so that the same script can be used for all the files that will be processed.

On bash backsticks works

I usualy do something like

PIP_PATH=`python -c "from distutils.sysconfig \
import get_python_lib; print(get_python_lib())"`


Passing variable from shell script to python script, I have a shell script which inturn call the python script, I want to pass two variables from shell script to python script for its execution. How do i  I have a bash script, , and in it I have call a python script . The python script calculates something, and I want it to return a value that will be used later in . I know I can do. In var=`python` In print x # when x is the value I want to pass But this is not so convenient, because I also print other messages

Pass bash variable to python, I want to use this script to execute on other directory and pass directory path with file name through command line. Any Tagged: shell scripts. Discussion started​  $ python arg1 arg2 arg3 The Python sys module provides access to any command-line arguments via the sys.argv. This serves two purposes − sys.argv is the list of command-line arguments. len(sys.argv) is the number of command-line arguments. Here sys.argv[0] is the program ie. script name. Example. Consider the following script −

BASH Shell Scripting: Passing Arguments to a Python Script, In this short blog I write a small Python utility script to create directories and demonstrate how to utilize it and pass it arguments via BASH shell  Set and Use Environment Variable inside Python Script. I was working on one of the python script to connect to Hadoop Hive server2, our requirement was to use Hive JDBC driver and that require CLASSPATH bash environment variable to be set to Hadoop native jar files path. Below are the steps that I followed to set and use CLASSPATH environment

How to pass a python variables to shell script.?, How to pass a python variables to shell databricks notebook, The python parameters can passed from the 1 st cmd to next %sh cmd .

  • Well, maybe you could not do the batch script to starty with - do everything from Python, from where the disctintion between data and code is always clear, you don't have to spawn a new proccess simply to read a scalar value (like the answers using "cat" bellow), and so on.
  • :-) Although I'd argue it's not ugly at all. Keeping information in files is exactly what files are for. It allows for much easier debugging in a multi-program solution to a problem.
  • Perhaps "ugly" was too strong... I perceive files to be more for long(er)-term storage of data. Writing something to disk then reading it right back milliseconds later as a means of passing around data in what is conceptually a single program seems inelegant to me (surely this is what RAM is for?). There's also about 10 billion things that can go wrong when doing file I/O (though to be fair, usually things work perfectly). On the other hand, it works!(And the other alternatives are even less savoury (or more complex) -- IPC in Bash, anyone?)
  • @Cameron: IPC? Named pipes, process substitution and (in Bash 4) coprocesses.
  • @Dennis: Thanks for the info (the only one of those I'd heard of was named pipes), but my point was that doing file I/O is easier than any of these techniques (powerful though they may be).
  • This was my other thought. I decided files felt more 'correct' in that the other messages are not all "error"-ey enough based on what I could tell from the OP.