How to redirect stdout from right to left

A simple and proper way can be to define a foobar function,

foobar () { ./foo "$@" | ./bar ; }

(either in the command line, as needed, or in some startup scripts, such as ".bashrc" for example). Then whenever you do :

foobar "this"  that   "and this also"

it will do

./foo  this   that   "and this also" | ./bar

I think you could do something like that with a named pipe:

mypipe=$(mktemp -d /tmp/XXXXXX)/pipe
mkfifo $mypipe
grep something < $mypipe & tail > $mypipe -f  some_input_file 
rm $mypipe

or put the unchanging part of the command in a shell script:

./foo "$@" | ./bar

and give the arguments to foo on the script command line:

./myscript <args to foo>

./bar < <( ./foo )

For example: cat < <(echo "hello there!")

To understand how it works, consider parts of the script separately.

This syntax: cat < /path/to/file will read the file /path/to/file and pipe it as stdin to cat.

This syntax: <(echo "hello there!") means to execute the command and attach the stdout to a file descriptor like /dev/fd/65. The result of the whole expression is a text like /dev/fd/65, and a command run in parallel and feeding that file descriptor.

Now, combined, the script will run the right command, pipe it to a file descriptor, convert that file descriptor to stdin for the left command, and execute the left command.

There is no overhead that I'd know of, it's exactly the same as a | b, just syntactic sugar.

Tags:

Bash

Pipe

Zsh