Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

Currently trying to pass a parameter from my python script to a bash script I created. How can I can get user input from my python script to my bash script?

This is the code for my python script 'passingParameters.py' which I used to try and send a variable (loop) to my bash script. I have tested this python script (after I adjusted the code) by sending the output to another python script which I used to read the input.

loop = str(sys.argv[1])
subprocess.check_call( ["./test.sh", loop], shell=True)

This is the code for my bash script 'test.sh'. I have tested this script by itself to confirm that it does receive user input when I just call the bash script from the command line.

echo "This number was sent from the passParameters.py script: " $1
share|improve this question
1  
Pretty related: How do I pass a Python Variable to Bash? – fedorqui Aug 5 '14 at 13:44
1  
The code should work. Do you get an error? Or why do you think the parameter isn't passed? – Aaron Digulla Aug 5 '14 at 13:57
    
@AaronDigulla no error, just the value I input into the command line doesn't show up – MarkOSullivan94 Aug 5 '14 at 14:27
    
@MOS182 See my comment to crono's answer. – chepner Aug 5 '14 at 14:32

1 Answer 1

up vote 4 down vote accepted

If you use shell=True then the executable is /bin/sh, which then calls test.sh but never sees the loop variable. You can either set shell=False and add the #!/bin/sh to the shell script,

#! /bin/sh
echo "This number was sent from the passParameters.py script: " $1

and

subprocess.check_call( ["./test.sh", loop], shell=False)

or pass the variable as a string:

subprocess.check_call( ["./test.sh %s" % loop], shell=True)

shell=True is not recommended anyway.

share|improve this answer
    
+1 It's not well documented, but combining your list of arguments with shell=True will result in the following command: sh -c ./test.sh <loop>. This will still run ./test.sh, but the argument loop is passed to sh as $0 rather than to ./test.sh as $1. – chepner Aug 5 '14 at 14:31
    
Thanks for your help! This first suggestion works fine. I tried the second suggestion with shell=True and it didn't work, says invalid syntax for the % beside loop. – MarkOSullivan94 Aug 5 '14 at 14:35
    
@chepner that's what I thought might have explained why it wasn't displayed so instead of having $1 in the bash script, I tried $0 and it still didn't print out anything. – MarkOSullivan94 Aug 5 '14 at 14:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.