Why does piping cause delayed expansion?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Queue
Posts: 31
Joined: 16 Feb 2013 14:31

Why does piping cause delayed expansion?

#1 Post by Queue » 16 Feb 2013 15:10

A simple example:

Code: Select all

set A=B
echo %%A%%
echo|echo %%A%%
echo|echo %%%%A%%%%

outputs:

Code: Select all

%A%
B
%B%

(echo| could be any command that doesn't make cmd.exe abort or output to stderr, like cd;.|, type nul|, etc.)

The third output line confuses me; why doesn't it output %A%?

Queue

Squashman
Expert
Posts: 4114
Joined: 23 Dec 2011 13:59

Re: Why does piping cause delayed expansion?

#2 Post by Squashman » 16 Feb 2013 15:20

Read Jeb's explanation of how the cmd parser works.
http://stackoverflow.com/questions/4094 ... 33#4095133

jeb
Expert
Posts: 918
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Why does piping cause delayed expansion?

#3 Post by jeb » 16 Feb 2013 15:33

Squashman wrote:Read Jeb's explanation of how the cmd parser works.

Will not help here, as this part isn't expained there.

But this question is nearly the same (or better, the answer is the same)
Why does delayed expansion fail when inside a piped block of code?

The behaviour looks very strang here, but if you know the cause it's obvoius :)

Both sides of a pipe are executed in a new cmd.exe context.
And both sides are executed like a command at the command prompt, that is important, as executing commands at the command prompt are handled different than commands in batch files.
(Ok at this point the parser explanation could be helpful).

Effectivly there are two phases in you examples.

The first phase is in the batch file, the standard percent expansion rules.

Code: Select all

anyCmd | echo Test2 %%A%%
anyCmd | echo Test3 %%%%A%%%%
->
anyCmd | echo Test2 %A%
anyCmd | echo Test3 %%A%%


The next phase is the percent expansion at the command prompt in the new cmd.exe context.
And here the percent expansion works different, as in batch files.

Code: Select all

echo Test2 %A%
echo Test3 %%A%%
->
echo Test2 B
echo Test3 %B%


The expansion in test3 looks strange, but it's normal at the command prompt.

Hope it helps

jeb

PS: btw. what a luck for me, that Dave isn't online now, so I had the time to answer an interesting question :D

dbenham
Expert
Posts: 2288
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Why does piping cause delayed expansion?

#4 Post by dbenham » 16 Feb 2013 16:19

jeb wrote:PS: btw. what a luck for me, that Dave isn't online now, so I had the time to answer an interesting question :D

Too funny :lol:

Queue
Posts: 31
Joined: 16 Feb 2013 14:31

Re: Why does piping cause delayed expansion?

#5 Post by Queue » 16 Feb 2013 16:40

Ah ha! Thanks. I hadn't quite put 2 and 2 together; particularly, I hadn't grasped in what way it'd mix both batch and cmd parsing.

I've been referencing your guys' work (particularly on stackoverflow) for what feels like years at this point, but only recently realized this was where a good bit of batch experimenting went on.

Queue

Post Reply