Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch: Escaping with caret

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?

like image 428
Jori Avatar asked Jun 25 '12 16:06

Jori


2 Answers

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 ^& | more actually attempts to execute

C:\Windows\system32\cmd.exe /S /D /c" echo & "

on the left and

C:\Windows\system32\cmd.exe /S /D /c" more "

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 ^^^& | more becomes

C:\Windows\system32\cmd.exe /S /D /c" echo ^& "

There 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.

like image 134
dbenham Avatar answered Sep 18 '22 17:09

dbenham


The first ^ is escaping the ^ character itself (the second ^) and the third ^ is escaping the &.

When you run a command like ECHO ^& | MORE, the ^& is replaced with & by the shell before the output is piped to MORE.

So, when you run ECHO ^^^& | MORE, the shell replaces ^^ with ^ and the ^& with & and then pipes the output to MORE.

like image 30
aphoria Avatar answered Sep 20 '22 17:09

aphoria