0

HOw can I pipe in result of which somecommand to outerCommand so as to replicate result of what I will do in shell as outercommand 'which somecommand'

Chang Zhao
  • 631
  • 2
  • 8
  • 24

1 Answers1

0

According to the suggestions of the documentation, I would avoid using the low-level interface Popen, which is tricky to use correctly in the long run, and to mindlessly use shell=True, which might expose you to shell injection attacks.

I would therefore suggest a simpler approach if that's all you need: just capture the output and use it using the new capture_output parameter of the new subprocess.run() API.

This will get what you need:

completed_process = subprocess.run(['outercommand', subprocess.run(['which', 'somecommand'], capture_output=True).stdout], capture_output=True)

completed_process.check_returncode()
print(completed_process.stdout.decode('utf-8'))