May 18th, 2006

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%

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.