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

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 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:


    >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


    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.