Shell Pipes

    [renegade] how does I/O redirection work in context of pipes?
    [gef]      I'm unclear on how | is different than <
    [renegade] cat|more<foo
    [thrig]    | is between programs, < is from a file 

Complications arise in that < or > file redirections can be moved around within the command, to some degree; the following are all equivalent, to put "foo" into the file "bar", though the last is the typical form seen, and the others might get you frowny faces on a code review.

    $ > bar echo foo
    $ echo > bar foo
    $ echo foo > bar

"cat | more < foo" is more complicated, and may require a study of the source code or standards to see how it behaves. The problem here is that there is only one standard input, and the pipe from "cat" conflicts with the input redirection from "foo": both demand exclusive use of stdin, as shown below. This shell gives preference to the "cat < bar" over the "echo | cat". If you need both stdin and input from a file, then cat(1) can combine those in some order.

    $ echo bar > bar
    $ echo foo | cat < bar
    bar
    $ echo foo | (cat - bar | more)
    ...

Another issue is what exactly a program does with its arguments.

    [thrig] < file foo | bar  is totally fine, foo gets file on stdin,
            bar gets foo on stdin
    [gef]   and it wouldn't be the same a foo file | bar ?

"foo < file" may or may not be the same as "foo file", as this depends on how "foo" is written. Various programs on unix do treat no arguments as an excuse to read from standard input, though other programs do not. Some will require an explicit "-" argument to indicate that stdin should be read from, while others will not read from stdin, or do not take filenames as arguments. Check the fine manual for details.

Link

Link


Source