Setup pipes inside wrapping bash script Setup pipes inside wrapping bash script shell shell

Setup pipes inside wrapping bash script


In this particular case, you don't have to specify the pipes or redirection in your shell script. Pipes and redirection change the standard input, output, and error handles. Those handles are inherited by child processes unless you say otherwise. So when you run execute < input.txt, the standard input handle for both execute and the python it executes will be connected to input.txt. You only need to specify redirection or piping inside execute if you want to change what the caller of execute set up.

Note on terminology: <, >, and >> are redirection operators, not pipes. Only | is a pipe. The mechanisms are similar, but the pipe requires the shell to execute a new process.

Edit I do not know any way for a script to get the filenames associated with its stdin/stdout/stderr handles. In the case of a pipe, there are no such filenames!

Also, bash has a process substitution feature that is sort of a mix of pipes and redirection. execute < <(echo yes) will run echo yes, and put its output in a special filename (e.g., /dev/fd/42). The special filename goes on the command line. So the result is like echo yes > /dev/fd/42 ; execute < /dev/fd/42. This is useful when you don't want to create a new subshell, which | normally does.