percent symbol in windows bat file

Percent symbols are confusing in Windows bat scripts because they are designed with different meanings in different situations.

The most occurrences of percent symbols are like %xxx%, where xxx is considered as a variable name and %xxx% will be expanded to the value of the variable.

Now they want to give you a little confusion. You will see some occurrences of percent symbol like %1,%2,…. Note there is only one % in these cases, and the character following the percent symbol is a digit. %1,%2,… are considered as parameters to the script, and will be expanded to the actual parameters.

But they think it is still too easy to be understood by you. So they decide to use %x as the loop variable in For statement like:

for %g in (a,b) echo %g

The good logically consistent design would be

for g in (a,b) echo %g%

But they won’t use this logically consistent design and insist in using that logically inconsistent design.

They still think it is not hard enough for you, so they add more complexity. They use double percent symbols in bat scripts:

for %%g in (a,b) echo %%g

The single percent of the for loop works on command line while the double percent version of the for loop works in scripts. They give you a lot of reasons( such as this and this) why you should use two percents in script. But for me, they are just fooling you.

 

If you like my content, please consider buying me a coffee. Buy me a coffeeBuy me a coffee Thank you for your support!
Posted in

Leave a Reply