MS-DOS SHIFT Command: Handling Over 9 Parameters
In MS-DOS 6.22, batch files natively support only ten replacement
parameters at any given time, spanning from %0 through
%9. When a batch script requires more than nine
user-supplied command-line arguments, the SHIFT command
overcomes this limitation by shifting the position of all replaceable
parameters one step down, effectively discarding the first parameter and
making subsequent arguments accessible.
The Nine-Parameter Limit
MS-DOS assigns command-line inputs sequentially to internal variables:
%0represents the batch file's command name itself.%1through%9represent the first nine command-line arguments.
Because MS-DOS variable parsing evaluates single-digit tokens,
entering %10 does not target the tenth parameter. Instead,
the command interpreter evaluates %1 followed by a literal
character 0. This makes direct access to arguments beyond
the ninth impossible without shifting.
How the SHIFT Command Works
Executing SHIFT within a batch file alters the internal
assignment of all parameters:
- The value in
%0is permanently discarded. - The value in
%1moves into%0. - The value in
%2moves into%1. - Every subsequent parameter moves down by one index (
%nbecomes%n-1). - The previously unreachable tenth parameter shifts into
%9.
Repeatedly executing SHIFT continuously moves arguments
down the chain, allowing an arbitrary number of parameters to be read
sequentially.
Standard Implementation Pattern
Batch files typically process an unknown number of parameters by
placing SHIFT inside a loop that reads only
%1, shifts the values, and checks whether the next argument
is empty:
@ECHO OFF
:LOOP
IF "%1"=="" GOTO END
ECHO Processing parameter: %1
SHIFT
GOTO LOOP
:ENDIn this structure, the script inspects only %1 during
each iteration. When SHIFT executes, the former
%2 moves into %1, and the next unprocessed
argument from the command line moves into the visible range until no
arguments remain.
MS-DOS 6.22 Limitations
Unlike later Command Prompt implementations in modern Windows (which
introduced switches like SHIFT /2 to preserve leading
parameters), MS-DOS 6.22 supports only the bare SHIFT
keyword. It always overwrites %0, and once a parameter is
shifted off the left end of the index list, it cannot be recovered
without restarting the script with the original command-line
arguments.