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.

 

Did you like this?
Tip admin with Cryptocurrency

Donate Bitcoin to admin

Scan to Donate Bitcoin to admin
Scan the QR code or copy the address below into your wallet to send some bitcoin:

Donate Bitcoin Cash to admin

Scan to Donate Bitcoin Cash to admin
Scan the QR code or copy the address below into your wallet to send bitcoin:

Donate Ethereum to admin

Scan to Donate Ethereum to admin
Scan the QR code or copy the address below into your wallet to send some Ether:

Donate Litecoin to admin

Scan to Donate Litecoin to admin
Scan the QR code or copy the address below into your wallet to send some Litecoin:

Donate Monero to admin

Scan to Donate Monero to admin
Scan the QR code or copy the address below into your wallet to send some Monero:

Donate ZCash to admin

Scan to Donate ZCash to admin
Scan the QR code or copy the address below into your wallet to send some ZCash:
Posted in

Leave a Reply