In very lot of answers, mainly about text-processing
commands, I saw commands such as sed
, awk
, grep
, among other, being used with STDIN and the simple open of a file
e.g.
$ sed -e 's|foo|bar|g' file # open file
$ sed -e 's|foo|bar|g' <file # open STDIN
or
$ grep 'PATTERN' file # open file
$ grep 'PATTERN' <file # open STDIN
In a personal, I use the open file method always, but I want to know when and when not to use them, also what's the difference.
file
. The shell merely opensfile
then manipulates file descriptors (e.g. usingdup2
) so thatfile
is available on descriptor 0 (standard in). The shell will thenexec
grep
, replacing itself and allowinggrep
to read standard input as it pleases. – Barefoot IO Mar 04 '16 at 00:08