Shell Command Starting with ">" Shell Command Starting with ">" shell shell

Shell Command Starting with ">"


The "Bash Reference Manual" says the following about the redirection operators:

The following redirection operators may precede or appear anywhere within a simple command or may follow a command.

So the following commands are all equivalent:

ls -al > listing.txt> listing.txt ls -alls > listing.txt -al

Though I'd guess that the first is most common form.

Note that the relative order of redirections is significant, so if you're redirecting one file descriptor to another, for example, the following would be different:

ls > listing.txt 2>&1   # both stdout and stderr go in to listing.txtls 2>&1 > listing.txt   # only stdout goes to listing.txt, because stderr was made                        #    a copy of stdout before the redirection of stdout


The ">" simply redirects the output of the command to the file given as the next argument. The typical use is to append this at the end of the command but it can be in any place. So:

> outfile <command> is equivalent to <command> > outfile. Same holds true for the input redirection.

Please note that in the above examples <command> is not some weird syntax - it simply replaces arbitrary command.


The equivalent is not:

cat infile > outfile

Rather, it is

cat < infile > outfile

(just that in case of cat, cat infile is same as cat <infile)

Once you see it that way, the original command seems more straightforward.