Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does a bang character mean in a windows bat file?

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.

like image 238
Alex Blakemore Avatar asked Mar 14 '23 22:03

Alex Blakemore


1 Answers

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 \".

like image 125
jamesdlin Avatar answered Mar 19 '23 05:03

jamesdlin