The question is the answer. You're clearly using Microsoft's cmd
command interpreter in Windows NT; and in that command interpreter setlocal
indeed has no effect when invoked outwith a command script.
Other command interpreters differ, in what setlocal
does, in whether it can be invoked outwith a command script, in whether it nests, and in whether it controls the things that you are trying to control.
The original setlocal
, in Microsoft's cmd
in OS/2 version 1, just saved the current drive, directory, and environment; had no effect outwith command scripts; and didn't nest. As you can see, over the last quarter century the command has been extended in different directions.
One reason for a difference here is that JP Software's TCC performs variable expansion progressively in compound commands, and doesn't need the delayed variable expansion trick in order to do what you are trying to do. This:
for /l %i in (1,1,3) do (set text=%i & echo Hello %text%)
… prints what you want to print.
Further reading
SETLOCAL
. Windows XP Command-line reference A–Z. TechNet. Microsoft corporation.
SETLOCAL
. Take Command / TCC Help. JP Software.
- Jonathan de Boyne Pollard.
SETLOCAL
. 32-Bit Command Interpreter command reference.
SETLOCAL
. OS/2 Warp Command reference. IBM Corporation.
- J. Emmett Beam (1988). Illustrated OS/2. Wordware Publishing. ISBN 9781556220531. p. 40