5

I want to use the magic of subshells and redirection with the python subprocess module, but it doesn't seem to work, complaining about unexpected tokens are the parenthesis. For example, the command

cat <(head tmp)

when passed to 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)'
dugres
13.2k8 gold badges48 silver badges52 bronze badges
asked Sep 13, 2011 at 19:55
3
  • 1
    Notice that your errors are coming from /bin/sh. I think that syntax may only with in bash. Do you want subprocess to run bash? Is that the question? Commented Sep 13, 2011 at 19:59
  • @S.Lott I thought that on my installation, sh was bash. Certainly, if I did man sh, the man page that came up was bash. Scott Lamb pointed out that when invoked as sh, this feature is unavailable, so in a sense, yes, I wanted it run as bash, even though I thought it already was. Commented Sep 13, 2011 at 20:08
  • And why are you shelling out to the shell when Python can do what you want? Commented Sep 13, 2011 at 23:45

1 Answer 1

11

The <(head tmp) syntax is a bash feature called "process substitution". The basic/portable /bin/sh doesn't support it. (This is true even on systems where /bin/sh and /bin/bash are the same program; it doesn't allow this feature when invoked as plain /bin/sh so you won't inadvertently depend on a non-portable feature.)

>>> subprocess.Popen(["/bin/bash", "-c", "cat <(head tmp)"])
<subprocess.Popen object at 0x1004cca50>
answered Sep 13, 2011 at 20:01
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.