10

I had a problem with set not working in a batch file; it took a while to distil the problem; at first I thought it was to do with subroutine calls...

The script

@echo off
setlocal
set a=aaa
echo a = "%a%"
(
set b=bbb
echo b = "%b%"
)

produces the output

a = "aaa"
b = ""

whereas I'd expect

a = "aaa"
b = "bbb"

Why is this please? Is it a bug in DOS? Perhaps there's something about the (...) command grouping syntax that I'm unaware of.

Thanks.

jeb
  • 78,592
  • 17
  • 171
  • 225
Rhubbarb
  • 4,248
  • 6
  • 36
  • 40

3 Answers3

10

User delayed expansion and ! instead of %

@echo off
setlocal enableextensions enabledelayedexpansion
set a=aaa
echo a = "%a%"
(
set b=bbb
echo b = "!b!"
)
Andy Morris
  • 3,393
  • 1
  • 21
  • 20
  • Thanks for the concise reply. Do you happen to know if there's a syntax for the numbered parameters %1 etc.? I have tried !1 and !1!, but these don't work. (I'm asking this in the context of a :subroutine call.) – Rhubbarb Nov 06 '09 at 14:56
  • 1
    Since they can't change between parsing and execution the way %a% type variables can, I cant see that it matters. – Andy Morris Nov 06 '09 at 15:39
  • You're quite right. I had other problems with the script that then exhibited the same symptoms. Problem solved. Thanks very much (to all). – Rhubbarb Nov 06 '09 at 17:02
7

What's happening is that the batch interpreter as treating everything in between the brackets a single line. This means it's doing variable replacement on everything betweeen the brackets before any of the commands are run.

So:

(
set b=bbb
echo b = "%b%"
)

Becomes:

(
set b=bbb
echo b = ""
)

The variable b is being set but obviously isn't set before you run the SET command.

David Webb
  • 190,537
  • 57
  • 313
  • 299
6

You need delayed expansion to be on, or the batch interpreter will interpolate all variables at parsing time, instead of run time.

setlocal enableextensions enabledelayedexpansion

See this question for an example and some great explanation of it.

Community
  • 1
  • 1
Tomalak
  • 332,285
  • 67
  • 532
  • 628