ECHO OFF
setlocal
FOR /L %%A in (1,1,5) DO (
SET /a "B=%%A-1"
call ECHO %%B%%
)
Since you are not using setlocal, B will be set to the value from the previous run. %B% will be replaced by 4 since B was set to 4 by the previous run. the call echo trick uses a parsing quirk to retrieve the current (run-time) value of the variable.
Here's "the official" way:
ECHO OFF
setlocal enabledelayedexpansion
FOR /L %%A in (1,1,5) DO (
SET /a "B=%%A-1"
ECHO !B!
)
In delayedexpansion mode, !var! retrieves the value of var as it changes at run-time. This is not without its drawbacks, but you'd need to read up on delayedexpansion for a guide on that matter.
for /L %%A in (0,1,4)? If that's not practical, then you shouldsetlocal enabledelayedexpansionandecho !B!. For more info, dohelp setin a cmd console, paying attention to the section beginning "Finally, support for delayed environment variable expansion has been added."ECHO IS OFFfive times.ECHO IS OFFfive times the first time I ran it, but ouput4five times the next time I ran it.