Command Line – Difference Between ‘>> file command’ and ‘command >> file’

bashcommand lineorderingredirection

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

>> file command
Does it mean that all stdout is redirected, not only that from command?

Such redirection affects a simple command. From man 1 bash:

Before a command is executed, its input and output may be redirected using a special notation interpreted by the shell. […] The following redirection operators may precede or appear anywhere within a simple command or may follow a command. Redirections are processed in the order they appear, from left to right.

"The following redirection operators" are [n]<word, [n]>word, [n]>>word etc.

A simple command is a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator.

And

control operator
A token that performs a control function. It is one of the following symbols:
|| & && ; ;; ( ) | |& <newline>

This means the following commands are equivalent:

echo Some text >> file
echo Some >> file text
echo >> file Some text
>> file echo Some text

The question is tagged and I quote man 1 bash but the above commands work in sh 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.


When and why would you reverse the order?

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:

grep foo file | tail | …

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:

my_custom_command | grep foo | tail | …
#                   ^^^^^^^^^^^^^^^^^^^ this is the part you'd be happy to paste

You need to remove file from the copied command. For this reason some users prefer posting commands like this:

cat file | grep foo | tail | …
#          ^^^^^^^^^^^^^^^^^^^ it's easy to copy this
#        ^^^^^^^^^^^^^^^^^^^^^ or even this

In other circumstances this would be totally useless use of cat. Some would say it still is, regardless of the circumstances. How about:

< file grep foo | tail | …
#      ^^^^^^^^^^^^^^^^^^^ it's easy to copy this

No cat and a convenient form!