The problem is with shell=True. Either remove that argument, or pass all arguments as a string, as follows:

Process=Popen('./childdir/execute.sh %s %s' % (str(var1),str(var2),), shell=True)

The shell will only pass the arguments you provide in the 1st argument of Popen to the process, as it does the interpretation of arguments itself. See a similar question answered here. What actually happens is your shell script gets no arguments, so 2 are empty.

Popen will inherit stdout and stderr from the python script, so usually there's no need to provide the stdin= and stderr= arguments to Popen (unless you run the script with output redirection, such as >). You should do this only if you need to read the output inside the python script, and manipulate it somehow.

If all you need is to get the output (and don't mind running synchronously), I'd recommend trying check_output, as it is easier to get output than Popen:

output = subprocess.check_output(['./childdir/execute.sh',str(var1),str(var2)])
print(output)

Notice that check_output and check_call have the same rules for the shell= argument as Popen.

Answer from micromoses on Stack Overflow
Top answer
1 of 3
19

The problem is with shell=True. Either remove that argument, or pass all arguments as a string, as follows:

Process=Popen('./childdir/execute.sh %s %s' % (str(var1),str(var2),), shell=True)

The shell will only pass the arguments you provide in the 1st argument of Popen to the process, as it does the interpretation of arguments itself. See a similar question answered here. What actually happens is your shell script gets no arguments, so 2 are empty.

Popen will inherit stdout and stderr from the python script, so usually there's no need to provide the stdin= and stderr= arguments to Popen (unless you run the script with output redirection, such as >). You should do this only if you need to read the output inside the python script, and manipulate it somehow.

If all you need is to get the output (and don't mind running synchronously), I'd recommend trying check_output, as it is easier to get output than Popen:

output = subprocess.check_output(['./childdir/execute.sh',str(var1),str(var2)])
print(output)

Notice that check_output and check_call have the same rules for the shell= argument as Popen.

2 of 3
3

you actually are sending the arguments ... if your shell script wrote a file instead of printing you would see it. you need to communicate to see your printed output from the script ...

from subprocess import Popen,PIPE

Process=Popen(['./childdir/execute.sh',str(var1),str(var2)],shell=True,stdin=PIPE,stderr=PIPE)
print Process.communicate() #now you should see your output
Discussions

bash - How to call a shell script file with variable argument through python - Unix & Linux Stack Exchange
I am calling a shell script file through a python command. Filename1 is a variable I have created in my standalone python program storing the actual value of filename. How do I write it to execute ... More on unix.stackexchange.com
🌐 unix.stackexchange.com
September 3, 2018
linux - Call Python script from bash with argument - Stack Overflow
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 scr... More on stackoverflow.com
🌐 stackoverflow.com
Execute a file with arguments in Python shell - Stack Overflow
I would like to run a command in Python Shell to execute a file with an argument. For example: execfile("abc.py") but how to add 2 arguments? More on stackoverflow.com
🌐 stackoverflow.com
pass arguments to python program in shell - Unix & Linux Stack Exchange
I am unable to run python script within a shell script. Following is the script.sh file where 3 arguments are being passed to the python script. More on unix.stackexchange.com
🌐 unix.stackexchange.com
June 12, 2016
🌐
Medium
medium.com › @evaGachirwa › running-python-script-with-arguments-in-the-command-line-93dfa5f10eff
Running Python script with Arguments in the command line | by Eva Mwangi | Medium
April 22, 2023 - Script that adds 3 numbers from CMD optional arguments: -h, --help show this help message and exit --num1 NUM1 Enter third number --num2 NUM2 Enter second number --num3 NUM3 Enter first number · default The default value will be used any time an argument is not provided.
🌐
Stack Exchange
unix.stackexchange.com › questions › 466543 › how-to-call-a-shell-script-file-with-variable-argument-through-python
bash - How to call a shell script file with variable argument through python - Unix & Linux Stack Exchange
September 3, 2018 - If I keep the actual filename, the shell script file gets executed through second statement. I need to automate this and cannot hardcode the actual filename. Thus, I want if by any chance the variable can be accessed in 2nd statement. import os os.system('sh uploadPDFContentFile.sh Filename1') ... Better use subprocess module. >>> subprocess.run(["ls", "-l"]) # doesn't capture output CompletedProcess(args=['ls', '-l'], returncode=0)
🌐
Java2Blog
java2blog.com › home › python › call python script from bash with arguments
Call Python Script from Bash with Arguments [2 Ways] - Java2Blog
April 14, 2023 - Then, the python command called our Python script my_code.py. Finally, we passed the arguments hello and world along with -a and -b. The string parameters were passed as the type of arguments were set to str. The code generates the error if a user tries to enter a number. Use python or python3 to run the my_code.py as per the installed python version.
Find elsewhere
🌐
Humairahmed
humairahmed.com › blog
BASH Shell Scripting: Passing Arguments to a Python Script | HumairAhmed.com
September 1, 2014 - The BASH shell script file takes in a argument(s) and passes the argument(s) to the Python script file. I make the BASH script file an executable with the “chmod +x [BASH script name]” command; I can then put this small utility script in one my path directories as shown by the “echo $PATH” command and call it anytime as needed from the shell.
🌐
Tutorialspoint
tutorialspoint.com › python › python_command_line_arguments.htm
Python - Command-Line Arguments
C:\Python311>python parser1.py C:\Python311>python parser1.py -h usage: parser1.py [-h] sample argument parser options: -h, --help show this help message and exit · The second command line usage gives −help option which produces a help message as shown. The −help parameter is available by default. Now let us define an argument which is mandatory for the script to run and if not given script should throw error.
🌐
IQCode
iqcode.com › code › python › shell-script-to-run-python-script-with-arguments
shell script to run python script with arguments Code Example
October 19, 2021 - #shell command >> python python_script.py var1 var2 # To access the variables inside the script import sys print sys.argv[0] # prints python_script.py print sys.argv[1] # prints var1 print sys.argv[2] # prints var2 ... Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond. Sign up · Develop soft skills on BrainApps Complete the IQ Test ... bash script with arguments in python run bash script with arguments in script python how to execute python script with arguments HOW TO RUN python script with command line arguments python pass argument to bash script pyt
🌐
Finxter
blog.finxter.com › how-to-execute-a-python-file-with-arguments-in-python
How to Execute a Python File with Arguments in Python? – Be on the Right Side of Change
This variable is defined on operating system level, so you can pass it within your calling Python script and fill it with the desired arguments. Fill the variable sys.argv with your desired values in the calling Python script. Load the Python file script.py into a Python string. Pass the Python ...
🌐
LinuxConfig
linuxconfig.org › home › how to use a bash script to run your python scripts
How to Use a Bash Script to Run Your Python Scripts
September 21, 2025 - The arguments will be accessible within the Python script through the sys.argv list. Here’s an example: #!/bin/bash cd /path/to/python/script/directory python myscript.py $1 $2 $3 · In this script, we run the myscript.py Python script and pass three arguments ($1, $2, and $3) to it.
🌐
Iditect
iditect.com › faq › python › execute-a-file-with-arguments-in-python-shell.html
Execute a file with arguments in Python shell
To execute a Python script with arguments in the Python shell, you can use the exec function along with the sys.argv list from the sys module.
🌐
Xojo Programming Forum
forum.xojo.com › general
Passing parameters to Python script with Shell.Execute - General - Xojo Programming Forum
June 7, 2018 - Mac OS 10.13.5 Xojo 2018 r1.1 My goal is to process some data in Python using the Shell.Execute command. I am passing that data as “parameters” of the Shell.Execute command. As I understand it, you can consider the string that is passed in the Shell.Execute command as a bunch of space-delimited “parameters”. If you want to fire up a Python script, then the first “parameter” I am passing is an absolute path to Python on my machine.
🌐
Medium
medium.com › @nawazmohtashim › call-python-script-from-bash-909afd5248a3
Call Python Script from Bash. In the world of software development… | by Mohd Mohtashim Nawaz | Medium
December 21, 2023 - Please enter a value." read -p "$1: " input done echo "$input" } # Get user input for arguments arg1=$(get_user_input "Enter argument 1") arg2=$(get_user_input "Enter argument 2") arg3=$(get_user_input "Enter argument 3") # Call the Python script with user-provided arguments python3 myscript.py "$arg1" "$arg2" "$arg3"
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-run-bash-script-in-python
How to run bash script in Python? - GeeksforGeeks
July 23, 2025 - A new process is created, and command echo is invoked with the argument "Geeks for geeks". Although, the command's result is not captured by the python script. We can do it by adding optional keyword argument capture_output=True to run the function, or by invoking check_output function from the same module.
🌐
Janakiev
janakiev.com › blog › python-shell-commands
How to Execute Shell Commands with Python - njanakiev
April 22, 2019 - The main function you want to keep in mind if you use Python >= 3.5 is subprocess.run(), but before we get there let’s go through the functionality of the subprocess module. The subprocess.Popen() class is responsible for the creation and management of the executed process. In contrast to the previous functions, this class executes only a single command with arguments as a list.
🌐
Keyboard Maestro
forum.keyboardmaestro.com › questions & suggestions
Execute Shell Script with Argument 😧 - Questions & Suggestions - Keyboard Maestro Discourse
April 22, 2018 - What I have missed? Or maybe a bug... Execute Shell Script with Argument 😧.kmmacros (2.9 KB) -alain