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

Raymond Chen

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%

0 comments

Discussion is closed.

Feedback usabilla icon