Python subprocess, subshells and redirection

I want to use subshell magic and redirection with the python subprocess module, but this doesn't seem to work, complaining about unexpected tokens, these are brackets. For example, the command

cat <(head tmp)

when passed to the subprocess gives this

 >>> subprocess.Popen("cat <(head tmp)", shell=True) <subprocess.Popen object at 0x2b9bfef30350> >>> /bin/sh: -c: line 0: syntax error near unexpected token `(' /bin/sh: -c: line 0: `cat <(head tmp)' 
+4
source share
1 answer

The <(head tmp) syntax is a bash function called process overriding. Basic / portable /bin/sh does not support it. (This is true even on systems where /bin/sh and /bin/bash are the same program, but do not allow this function when called as a simple /bin/sh , so you will not accidentally depend on a non-portable function.)

 >>> subprocess.Popen(["/bin/bash", "-c", "cat <(head tmp)"]) <subprocess.Popen object at 0x1004cca50> 
+10
source

All Articles