How delayed expansion works?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
lockedscope
Posts: 17
Joined: 12 Jun 2020 10:38

How delayed expansion works?

#1 Post by lockedscope » 13 Jun 2020 06:05

According the parsing rules (https://stackoverflow.com/questions/409 ... ts/4095133) is following explanation correct?

Code: Select all

set "a=xyz"
echo "a: !a!"

set "b=^!a^!"
echo "b: !b!"
rem Output: "b: !a!" - Only Delayed expansion

echo "b percent: %b%"
rem echo "b percent: !a!" - Percent expansion takes place expands %b% to !a!
rem "b percent: xyz" - Then delayed expansion takes place and expand !a! to xyz
rem Finally echo xyz executes

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

Re: How delayed expansion works?

#2 Post by jeb » 13 Jun 2020 10:31

Hi lockedscope,

yes your eplanation is correct.
Percent expansion is always the first one and can't be suppressed.
Then the for-variables expansion occurs.
And the last expansion can be the delayed expansion, if enabled.

jeb

lockedscope
Posts: 17
Joined: 12 Jun 2020 10:38

Re: How delayed expansion works?

#3 Post by lockedscope » 15 Jun 2020 08:06

Thanks jeb
jeb wrote:
13 Jun 2020 10:31
Hi lockedscope,

yes your eplanation is correct.
Percent expansion is always the first one and can't be suppressed.
Then the for-variables expansion occurs.
And the last expansion can be the delayed expansion, if enabled.

jeb

Aacini
Expert
Posts: 1647
Joined: 06 Dec 2011 22:15
Location: México City, México

Re: How delayed expansion works?

#4 Post by Aacini » 15 Jun 2020 16:33

Hi lockedscope,

If you want to review some strange things that can be achieved combining %normal% and !delayed! expansions, then I invite you to review this crazy thread...

Antonio

PS - Remember that several %%expansions%% can be nested via CALL command, but !delayed! expansion happen just once...

Post Reply