Code: Select all
rem @echo off
setlocal enableDelayedExpansion
set text=my
set "a=^^^^^^^!text^^^^^^^!"
echo "a: %a%"
rem echo "a: ^^^!text^^^!"
rem "a: ^!text^!"
set "b=%a%"
rem set "b=^^^!text^^^!"
echo "b: %b%"
rem echo "b: ^!text^!"
rem "b: !text!"
set "c=%b%"
rem set "c=^!text^!" - First %b% expands to "^^^!text^^^!"(check the "set b=%a%" above) and then resolving of carets(Phase 2) to single carets "^!text^!" and
rem at delayed expansion (phase 5), carets resolved so its resolved to !text! literally (So, value !text! of is not expanded because carets already resolved from ^! to ! in this phase).
echo c: %c%
rem echo c: !text! - Previous expansion/unescaping(resolving of caret) returns !text! literally and delayed expansion expands it to "my" value.
rem c: my
set "d=%c%"
rem set "d=!text!" - So, we understand that %c% still contains !text! value literally although it still echoes "my". Check the echo %d% below
echo d: %d%
rem echo d: my - Because when we echo %c%, it first "echo !text!" and "my" value but now with echo d we get "echo my" and so "my" directly without !text!.
rem d: my
goto :eof