Correct Way In Python To Write A Bash For Loop With `subprocess`
In this other SO question I saw that this syntax of bash when writing a for loop doesn't seem to work in Python: ~ $ for num in {2..4}; do echo $num; done 2 3 4 As instead of 4 di
Solution 1:
Post How to use the bash shell with Python's subprocess module instead of /bin/sh shows that it uses /bin/sh
but you can use executable='/bin/bash'
to change it
import subprocess
print(subprocess.check_output("for num in {2..4}; do echo $num; done", shell=True, executable='/bin/bash').decode())
Eventually you can use bash -c "command"
import subprocess
print(subprocess.check_output('bash -c "for num in {2..4}; do echo $num; done" ', shell=True).decode())
Post a Comment for "Correct Way In Python To Write A Bash For Loop With `subprocess`"