The redirection operator can occur in the middle of the command line

Date:May 18, 2006 / year-entry #171
Tags:tipssupport
Orig Link:https://blogs.msdn.microsoft.com/oldnewthing/20060518-07/?p=31153
Comments:    10
Summary:Although the redirection operator traditionally appears at the end of a command line, there is no requirement that it do so. All of these commands are equivalent: echo A B>C echo A>C B echo>C A B >C echo A B All of them echo "A B" to the file "C". You can use this trick...

Although the redirection operator traditionally appears at the end of a command line, there is no requirement that it do so. All of these commands are equivalent:

echo A B>C
echo A>C B
echo>C A B
>C echo A B

All of them echo "A B" to the file "C".

You can use this trick to avoid the redirection problem we discussed last time. We saw that writing

set message=Meet at 2
echo %message%>schedule

inadvertently interprets the "2" as part of the redirection operator. One solution was to insert a space:

echo %message% >schedule

but this assumes that the space won't cause a problem. If you're in a case where that space will indeed cause a problem, you can use the trick above to move the redirection operator to a location where it won't cause any trouble:

>schedule echo %message%

Comments (10)
  1. Anders says:

    And how far back does this work, NT(cmd.exe only)? 95/98? DOS?

  2. BryanK says:

    IIRC, it worked that way back in the command.com from DOS 5 or so.  Not sure about earlier.

    (Of course in anything pre-cmd.exe, you can’t redirect stderr anyway, but putting a redirection at the beginning of the command did work.  Actually in NT 4, you may not be able to redirect stderr either, but I’m not sure on that one.)

  3. Anders: Feel free to report back with your findings.

  4. Neil says:

    Anders,

    >dir.txt dir

    works in Windows 95.

  5. boxmonkey says:

    I think my brain just exploded.

    Not really. This is pretty cool, but I can see how it would be used for evil (obfuscation).

  6. Gabe says:

    The way it works is that the token immediately following a < or > is a filename for redirection, while everything else is part of the command to be executed. The parser has no reason to care where in the command the redirection token occurs.

    I'm pretty sure that it just always worked this way, ever since redirection was first implemented. In fact, I suspect that this goes all the way to when the syntax was first created in Unix, around 1972.

    Don't forget that Microsoft did the port of Unix to microcomputers shortly before DOS was released, so there would have been plenty of people around with Unix experience.

    [Yup, this feature has been around since the Thompson shell. "Such a redirection may appear anywhere among the arguments of a simple command." -Raymond]
  7. I think quoting is inevitable at some point.

    Consider what happens if %message% contains a >

  8. Mike Dunn says:

    My head a splode.

  9. msc says:

    However these command produce different results:

    >dirsort.txt dir /b | sort /r

    and

    dir /b | sort /r > dirsort.txt

  10. BryanK says:

    Well, um, yes, because in the first the redirection to dirsort.txt is part of the "dir" command.  In the second, it’s part of the "sort" command.

    So in the first, you’ve told the shell that stdout for the dir command should go to two different places (the file dirsort.txt and a pipe to another process).  If you don’t get an error, the shell probably does either the first or last, but it won’t do both.

Comments are closed.


*DISCLAIMER: I DO NOT OWN THIS CONTENT. If you are the owner and would like it removed, please contact me. The content herein is an archived reproduction of entries from Raymond Chen's "Old New Thing" Blog (most recent link is here). It may have slight formatting modifications for consistency and to improve readability.

WHY DID I DUPLICATE THIS CONTENT HERE? Let me first say this site has never had anything to sell and has never shown ads of any kind. I have nothing monetarily to gain by duplicating content here. Because I had made my own local copy of this content throughout the years, for ease of using tools like grep, I decided to put it online after I discovered some of the original content previously and publicly available, had disappeared approximately early to mid 2019. At the same time, I present the content in an easily accessible theme-agnostic way.

The information provided by Raymond's blog is, for all practical purposes, more authoritative on Windows Development than Microsoft's own MSDN documentation and should be considered supplemental reading to that documentation. The wealth of missing details provided by this blog that Microsoft could not or did not document about Windows over the years is vital enough, many would agree an online "backup" of these details is a necessary endeavor. Specifics include:

<-- Back to Old New Thing Archive Index