Home > Commands N-Z > Batch S

SHIFT


Description | Syntax | Parameters | Switches | Related | Notes | Examples | Errorlevels | Availability

Changes the position of replaceable parameters in a batch program.


Syntax

SHIFT /?

SHIFT [/n]


Parameters

none.


Switches
/? (NTXP)
Displays help.
/n (NT4)
Start shifting at the nth argument, where n may be between zero and eight.

Related

CALL - Call one batch program from another.
SET - Display or edit environment variables.
Equivalent Linux BASH commands:
shift - Shift positional parameters.


Notes
How the SHIFT command works

The SHIFT command changes the values of the replaceable parameters %0 through %9 by copying each parameter into the previous one. In other words, the value of %1 is copied to %0, the value of %2 is copied to %1, and so on. This is useful for writing a batch file that performs the same operation on any number of parameters.


Working with more than 10 command-line parameters

You can also use the SHIFT command to create a batch file that can accept more than 10 parameters. If you specify more than 10 parameters on the command-line, those that appear after the tenth (%9) will be shifted one at a time into %9.


Shifting parameters back

There is no backward SHIFT command. Once you carry out the SHIFT command, you cannot recover the first parameter (%0) that existed before the shift.


Examples

The batch file, MYCOPY.BAT, shows how to use the SHIFT command with any number of parameters. It copies a list of files to a specific directory. The parameters are the directory name followed by any number of filenames.

    @ECHO OFF
     REM MYCOPY.BAT copies any number of files to a directory.
     REM
     REM The command uses the following syntax:
     REM mycopy dir file1 file2 ...
     SET TODIR=%1
    :GETFILE
     SHIFT
     IF '%1'=='' GOTO END
     COPY %1 %TODIR%
     GOTO GETFILE
    :END
     SET TODIR=
     ECHO All done

Errorlevels

none.


Availability
Internal
DOS
v2.0 v2.01 v2.05 v2.10 v2.11 v2.11R v2.12 v2.2 v2.25 v3.0 v3.20 v3.05 v3.1 v3.21 v3.25 v3.30 v3.3A v3.3R v3.3T v3.31 v3.40 v4.0 v4.01 v4.01A v5.0 v5.0A v5.00.02 v5.001A v5.01 v5.02 v6.0 v6.10 v6.2 v6.21 v6.22 v6.23 v7.00 v7.0R1 v7.10 v8.00
Windows
Win95 Win98 WinME
Windows NT
NT3.5 NT4 NT2000 NTXP NT2003