I’ve always hated batch and I still do, even for the most simple things I prefer C or PHP/Perl. But this time I could not go without it, ****sigh****.
I wanted to redirect an echo command to an other command. For example:
echo example | more
but I also wanted to be able to use special characters in the echo part of the pipe:
echo & | more
Which of course did not work. So I tried:
echo ^& | more
Which did not work either. Then by trial-and-error I found:
echo ^^^& | more
and that worked. But as interested programmer I wonder why. Why did ^& not work and ^^^& did?
The reason has to do with how Windows implements pipes – Each side of the pipe is executed in its own CMD shell. Your command
echo ^& | moreactually attempts to executeon the left and
on the right. You can see why the left hand side fails – trying to echo an unescaped
&. The escape was consumed by the initial parsing phase before the actual left side is executed.It is also easy to see why your solution works. The left side of
echo ^^^& | morebecomesThere are many subtle complications when working with Windows pipes. Refer to Why does delayed expansion fail when inside a piped block of code? for more info. The selected answer has the best info, but I recommend reading the question and all answers to get the context of the selected answer.