4

I'm used to specify output redirection on the end of the line like:

echo blah > output.txt

Both in Windows and *nix.

Recently I'm seeing more and more examples that redirect at the beginning of the line, like:

> output.txt echo blah

Again, this seems to work both on Windows and *nix.

Is there any advantage to redirect the output at the beginning of a line? Does it behave identically to redirecting at the end of a line or is there some difference? Why are there even the two options?

4

For such simple examples it does not matter where the redirection happens. It's a choice of coding style here. The Bash manual states:

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

What is a simple command?

A simple command is the kind of command encountered most often. It’s just a sequence of words separated by blanks, terminated by one of the shell’s control operators (see Definitions). The first word generally specifies a command to be executed, with the rest of the words being that command’s arguments.

The order of redirection commands matters when there is more than one, obviously.

3

Suppose you want to write count: 1 to a file.

For Windows, echo count: 1>file.txt will write count : to the file (without the 1) because the 1 is interpreted to mean stdout. You could use echo count: 1 >file.txt, but then there is an unwanted trailing space. Placing the redirection at the front avoids the problem: >file.txt echo count: 1.

Not sure, if this also applies to 'nix, but I suspect it does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.