You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Vidar Holen edited this page May 3, 2020
·
1 revision
This redirection overrides piped input. To use both, merge or pass filenames.
Problematic code:
gzcat yesterday.log.gz | grep "$USER"< today.log
Correct code:
# Specify non-piped inputs as filenames
gzcat yesterday.log.gz | grep "$USER" - today.log
# Or merge multiple inputs into a single stream
{ gzcat yesterday.log.gz; cat today.log; } | grep "$USER"
Rationale:
A process only has a single standard input stream. Pipes and input redirections both overwrite it, so you can't use both at the same time. If you try, the redirection takes precedence and the input pipe is closed.
Many commands support specifying multiple filenames, where one can be stdin (canonically by specifying - as a filename, or alternatively by using /dev/stdin). In these cases, you can rewrite the command to use one piped input, and as many extra files (or process substitutions) as you want.
For commands that only process a single input stream (like tr), you can also concatenate multiple commands or files into a single stream using a { command group; } as in the example.
Exceptions:
None.
Related resources:
Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!