5

I'm trying to correct a bat file that uses bang characters where I would have expected some form of quote: something like

set some_var=!some_var:"=\"!

and then later

some command !some_var!

It looks like the first is prompting for input with echo disabled, like prompting for a password, and the second referencing the variable. Normally, bat files use percent characters for that, like

%some_var%

Anyone know what the bang character does in a bat file exactly? Trying to debug this thing, and don't like guessing.

Alex Blakemore
  • 11,301
  • 2
  • 26
  • 49

1 Answers1

7

From cmd /?:

If delayed environment variable expansion is enabled, then the exclamation character can be used to substitute the value of an environment variable at execution time.

(The cmd help describes elsewhere various methods of enabling delayed environment variable expansion.)

And from set /?:

Delayed environment variable expansion allows you to use a different character (the exclamation mark) to expand environment variables at execution time.

And

Environment variable substitution has been enhanced as follows:

%PATH:str1=str2%

would expand the PATH environment variable, substituting each occurrence of "str1" in the expanded result with "str2".

Your case of set some_var=!some_var:"=\"! is therefore updating some_var to replace " characters with \".

jamesdlin
  • 81,374
  • 13
  • 159
  • 204