Call Python script from bash with argument

PythonLinuxBashShellDebian

Python Problem Overview


I know that I can run a python script from my bash script using the following:

python python_script.py

But what about if I wanted to pass a variable / argument to my python script from my bash script. How can I do that?

Basically bash will work out a filename and then python will upload it, but I need to send the filename from bash to python when I call it.

Python Solutions


Solution 1 - Python

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 python_script.py var1 var2

To access these variables within python you will need

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

Solution 2 - Python

Beside sys.argv, also take a look at the argparse module, which helps define options and arguments for scripts.

> The argparse module makes it easy to write user-friendly command-line interfaces.

Solution 3 - Python

Use

python python_script.py filename

and in your Python script

import sys
print sys.argv[1]

Solution 4 - Python

Embedded option:

Wrap python code in a bash function.

#!/bin/bash

function current_datetime {
python - <<END
import datetime
print datetime.datetime.now()
END
}

# Call it
current_datetime

# Call it and capture the output
DT=$(current_datetime)
echo Current date and time: $DT

Use environment variables, to pass data into to your embedded python script.

#!/bin/bash

function line {
PYTHON_ARG="$1" python - <<END
import os
line_len = int(os.environ['PYTHON_ARG'])
print '-' * line_len
END
}

# Do it one way
line 80

# Do it another way
echo $(line 80)

http://bhfsteve.blogspot.se/2014/07/embedding-python-in-bash-scripts.html

Solution 5 - Python

use in the script:

echo $(python python_script.py arg1 arg2) > /dev/null

or

python python_script.py "string arg"  > /dev/null

The script will be executed without output.

Solution 6 - Python

I have a bash script that calls a small python routine to display a message window. As I need to use killall to stop the python script I can't use the above method as it would then mean running killall python which could take out other python programmes so I use

pythonprog.py "$argument" & # The & returns control straight to the bash script so must be outside the backticks. The preview of this message is showing it without "`" either side of the command for some reason.

As long as the python script will run from the cli by name rather than python pythonprog.py this works within the script. If you need more than one argument just use a space between each one within the quotes.

Solution 7 - Python

and take a look at the getopt module. It works quite good for me!

Solution 8 - Python

Print all args without the filename:

for i in range(1, len(sys.argv)):
print(sys.argv[i])

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJimmyView Question on Stackoverflow
Solution 1 - PythoniamthedrakeView Answer on Stackoverflow
Solution 2 - PythonmikuView Answer on Stackoverflow
Solution 3 - PythonNPEView Answer on Stackoverflow
Solution 4 - Pythonuser77115View Answer on Stackoverflow
Solution 5 - PythonburseanerView Answer on Stackoverflow
Solution 6 - PythonDave ThebuskerukView Answer on Stackoverflow
Solution 7 - PythonHappyHackingView Answer on Stackoverflow
Solution 8 - PythonRiadeView Answer on Stackoverflow