Python: subprocess and running a bash script with multiple arguments Python: subprocess and running a bash script with multiple arguments bash bash

Python: subprocess and running a bash script with multiple arguments


Pass arguments as a list, see the very first code example in the docs:

import subprocesssubprocess.check_call(['/my/file/path/programname.sh', 'arg1', 'arg2', arg3])

If arg3 is not a string; convert it to string before passing to check_call(): arg3 = str(arg3).


subprocess.Popen(['/my/file/path/programname.sh arg1 arg2 %s' % arg3], shell = True).

If you use shell = True the script and its arguments have to be passed as a string. Any other elements in the args sequence will be treated as arguments to the shell.

You can find the complete docs at http://docs.python.org/2/library/subprocess.html#subprocess.Popen.


Hi I know that this is solution is quite late, but could help someone.

example:

import subprocesspass_arg=[]pass_arg[0]="/home/test.sh"pass_arg[1]="arg1"pass_arg[2]="arg2"subprocess.check_call(pass_arg)

The above example provides arg1 and arg2 as parameters to the shell script test.sh

Essentially, subprocess expects an array. So you could populate an array and provide it as a parameter.