Difference Between % and %% in a Batch File

Siddharth Bishnoi Jan 30, 2023
  1. Use of % and %% in CMD
  2. Use of % and %% in a Batch File
  3. Use of Delayed Expansion in a Batch File
Difference Between % and %% in a Batch File

Batch programmers often confuse single percent (%) and double percent (%%) symbols when used in a Batch file. When executed on the command line, the FOR command uses %f, whereas, in a Batch file, it uses %%f instead of a single percent symbol.

The symbols %, %%, %variable% and !variable! have some basic differences and are used distinctly in the command line and Batch files.

This tutorial demonstrates the difference between % and %% in a Batch file. It also discusses the use of % and %% in Batch files and cmd, along with environmental variables and loop variables.

Use of % and %% in CMD

In a command-line shell, the %variable specifies a single letter replaceable parameter. Ms-DOS uses %1,%2,...,%9 as replaceable command line parameters, i.e., %1 will be replaced by the first parameter, passed to the Batch file.

The following example shows that a single percent symbol with a variable (%a) acts as a null character:

set a="Hello World"
echo %a

Use % in CMD

When the percent sign is used on both sides of a variable, i.e., %VARIABLE%, it acts as an environment variable and displays the set value when used with the echo command. The %VARIABLE% can be set with the set command and accessed with the echo command.

The example is shown below.

set a="Hello World"
echo %a%

Output - Use % in CMD

Use of % and %% in a Batch File

In Batch files, the command line shell reads all the commands from left to right. If it reads the percent sign, it proceeds by checking the next character.

It reads %% as a single %, i.e., replaces %% by a single percent and reads the third character. If it is in a FOR loop, it will evaluate it as in a FOR loop.

The following example explains the use of %% in a FOR loop.

FOR /L %%3 in (1,1,3) Do Echo %%3
PAUSE

Use of %% in for Loop

Output:

Output - Use of %% in for Loop

If it is not a FOR loop, it will read it as a plain character and run the same as the %variable command when directly executed in the command-line shell.

set a="Hello World"
echo %%a
PAUSE

Use of %% as Plain Character

Output:

Output - Use of %% as Plain Character

If the character after the % sign is a number, it will be read as a command line parameter. If the character after % is neither a % symbol nor a number, it will be read as a variable until the next % sign; then, it will display its value.

For example,

set a="Hello World"
echo %a%
PAUSE

Use of % Sign for Variables

Output:

Output - Use of % Sign for Variables

To use a plain % symbol in a Batch file like 50%, you need to use the %% instead of a single % sign.

set a="The battery is charged 50%%"
echo %a%
PAUSE

Read a % Sign in String

Output:

Output - Read a % Sign in String

The % and %% are primarily used in the FOR command. When the FOR command is used outside a Batch file, a single % sign works well.

But, when used within a Batch file, it should be replaced by double %% to avoid any errors. Also, it is recommended to avoid using numbers with the % sign as FOR command parameters.

Use of Delayed Expansion in a Batch File

Sometimes, using %VARIABLE% in a Batch file does not print the result. It is because when the percent expansion is done, the variable is expanded before the execution, i.e., at the parse time.

When using in loops, the variables are only expanded once.

To solve this, you can use delayed expansion, i.e., an exclamation mark (!variable!) instead of a percent sign (%variable%). The variable is expanded at execution time when the delayed expansion is enabled.

In a loop, it is expanded each time the loop is iterated. Therefore, expanding a variable using ! instead of % is recommended, mainly when loops are used.

To enable the delayed expansion, we can do it with the SETLOCAL EnableDelayedExpansion command and replace % with !, as shown in the example below.

@echo off
setlocal EnableDelayedExpansion
:: count to 5, storing results in a variable
set n=0
FOR /l %%G in (1,1,5) Do (echo [!n!] & set /a n+=1)
echo Total = %n%

Use of Delayed Expansion

Output:

Output - Use of Delayed Expansion

So, we discussed the difference between the % and %% signs when used in a Batch file and cmd. We covered almost everything, including the use of delayed expansion.

Related Article - Batch Command