3

I'm looking over some batch files written by a coworker, and I ran into some weird syntax where he calls redirection operator at the beginning of his command.

This:

> output.txt ECHO something like this

rather than:

ECHO something like this > output.txt

It threw me off for a second. It works, so I assume its valid. I went to look for an official reference to confirm, but could not find one.

I did find this reference on the usage of the operator itself.

I found this bash version of what I'm looking for linked to here: POSIX sh EBNF grammar

What is the correct syntax for the use of the > and >> operators in batch?

Edit: To clarify, I'm not looking for examples as documentation. I would like to see documentation explaining how cmd.exe commands and .bat files are parsed specifically in regards to redirection operators.

Community
  • 1
  • 1
Pete
  • 1,305
  • 1
  • 12
  • 36
  • 4
    `echo This is > file.txt also valid` – Aacini Jan 09 '17 at 20:55
  • 1
    Possible duplicate of [cmd.exe redirection operators order and position](http://stackoverflow.com/questions/25559389/cmd-exe-redirection-operators-order-and-position) – aschipfl Jan 10 '17 at 09:57
  • @aschipfl That question is asking for docs on redirection, but for a different aspect of behavior. Their question is regarding position and order relative to each other in the case that multiple redirection operators in the same command. – Pete Jan 11 '17 at 17:37
  • It's not only about the order relative to each other, it's also about the absolute position in the command line; see section "About position" in the [accepted answer](http://stackoverflow.com/a/25562437)... – aschipfl Jan 11 '17 at 17:47
  • I'm asking a different question, even if it had the same answer. They show an example of the redirection operator used at the beginning of the line. This doesn't really answer my question. I know it works, I'm just looking for some documentation on the cmd.exe parser's grammar. – Pete Jan 11 '17 at 18:07

1 Answers1

5

The official documentation for command redirection operators demonstrates their use at the end of the line. But putting the redirection at the beginning of the line is no less correct (piped output notwithstanding).

The benefit of putting the redirect at the beginning of an echo line is that you avoid echoing a trailing space into the file.

echo Hello world! > out.txt

15 bytes, includes a trailing space.

> out.txt echo Hello world!

14 bytes, does not include a trailing space

You can also use parentheses to avoid the trailing space.

(echo Hello world!) > out.txt

14 bytes

Parentheses can also be used to group the output of several commands without having to close and reopen the file handle for each line.

rem // option 1
> out.txt (
    echo Hello world!
    echo Another line
)

rem // option 2
(
    echo Hello world!
    echo Another line
) > out.txt

Both of those parenthetical code blocks perform the exact same action. And both are more efficient than this:

rem // option 3
>out.txt echo Hello world!
>>out.txt echo Another line

The parenthetical examples in options 1 and 2 above open, write, and close "out.txt" only one time; whereas the option 3 example opens, writes, closes, opens for appending, writes, and closes. When dumping a lot of data to a text file, such optimizations can make a difference.

rojo
  • 24,000
  • 5
  • 55
  • 101
  • Just as a comment: I recommend to put the redirections in the "logical" place: input redirection at beginning and output redirection at end: `< input.txt ( block & of & commands ) > output.txt` – Aacini Jan 09 '17 at 21:00
  • Really? I often do the opposite, e.g. `>output.txt set /P "=line without trailing CrLf" – rojo Jan 09 '17 at 21:02
  • You official doc is the same as what I linked to in the question. It defines how to use the operator, and I understand that. I was looking for something defining the syntax of a batch command, and where in that command the operator can be used. – Pete Jan 09 '17 at 21:08
  • @rojo: This is an entirely personal preference matter, but when the block is comprised of many lines, the input commands are usually placed at beginning and the output commands at end. In this order I find easier to match read commands "from" the file placed at beginning, and "write" commands "to" the file placed at end (like a block with "input at beginning and output at end"). – Aacini Jan 09 '17 at 21:11
  • @Pete You know how when you eat pizza, you're supposed to start at the tip and work your way to the crust? And if you eat your pizza any other way, you're shunned from society, possibly suspected of being a serial killer? Yeah, batch redirectors are nothing like that. Put them wherever makes sense to you. It's all part of the artistry and embellishment that goes into batch scripting. – rojo Jan 09 '17 at 22:40
  • @Aacini, the "more logical" place for me was `< input.txt > output.txt ( block & of & commands )`; you know why? because redirection is done *before* commands are executed... ;-) – aschipfl Jan 10 '17 at 09:56
  • 1
    @rojo: Your pizza comment remembers me the story about the origin of the terms used for byte-ordering in a larger data unit, taken from Jonathan Swift novel _[Gulliver's Travels](https://en.wikisource.org/wiki/Gulliver%27s_Travels/Part_I/Chapter_IV)_: In the kingdom of Lilliput there is an edict commanding all his subjects, upon great penalties, to break the smaller end of their eggs before eat them, so they are called "little-endians" (_smaller part first_). The renegades, called "Big-endians" (_biggest part first_), are exiled and take refuge in the kingdom of Blefuscu, enemy of Lilliput. – Aacini Jan 10 '17 at 18:49
  • @aschipfl: You are wrong. The whole block is first parsed, including the redirections no matter where they are placed, and then the redirections are enabled before the commands are executed... You should be exiled to Blefuscu for said that! **`^_^`** – Aacini Jan 10 '17 at 18:50