DOS batch: SET variable and ECHO it within (…) block

后端 未结 3 1209
梦毁少年i
梦毁少年i 2021-02-05 23:31

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 sc

3条回答
  •  滥情空心
    2021-02-05 23:48

    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.

提交回复
热议问题