Python: executing shell script with arguments(variable), but argument is not read in shell script

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 $1 and $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.


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