Picking up from this highly voted comment to What does `>>` mean in terminal command?:
"program before" What does that mean? Command obviously, but redirections can also be written prepended, i.e.
>> file command
I don't remember having seen such a case – although, given the amount of upvotes, they clearly exist. I have only ever seen and used redirection commands of the format
command >> file
when using >>
and its ilk (i.e. 2>
, 2>&1
, etc.).
When and why would you reverse the order? Does it mean that all stdout
is redirected, not only that from command
? Does anyone have any concrete examples?
I have had a google and could find any immediate examples.
Best Answer
Such redirection affects a simple command. From
man 1 bash
:"The following redirection operators" are
[n]<word
,[n]>word
,[n]>>word
etc.And
This means the following commands are equivalent:
The question is tagged bash and I quote
man 1 bash
but the above commands work insh
as well.The command line parser needs to "serve" all redirections before it runs the command (i.e. the command stripped of these redirections). Think about it, the procedure is the same regardless of where the particular redirection is. There's no reason to require it to be at the very end.
I don't recall a situation where I wanted to have a redirection in the middle. There is, however, a usage case where having an input redirection at the beginning is quite useful. Consider this code:
Imagine the pipe is very long. It's posted on Super User and solves some problem. You may paste it to your console and run it. What if you'd rather want to append it to some command or pipe? E.g. you want to get:
You need to remove
file
from the copied command. For this reason some users prefer posting commands like this:In other circumstances this would be totally useless use of
cat
. Some would say it still is, regardless of the circumstances. How about:No
cat
and a convenient form!