Programming Field

Shift - DOS/Command Prompt Reference

Shifts parameters ‘%0’ to ‘%9’ to smaller numbers in the batch program (‘%1’ → ‘%0’, ..., ‘%9’ → ‘%8’, (unusable 10th parameter) → ‘%9’).

Syntax

shift [/n]

Options

/n [Windows NT series] [Extensions] By specifying 0 to 8 to n, changes the start number to shift. For example, if you specify ‘/2’, ‘%0’ and ‘%1’ remains, and Shift performs the shifting as ‘%3’ → ‘%2’, ‘%4’ → ‘%3’, etc.

Details

Usage of Shift

In the batch program you can use 9 parameters of the program by using ‘%1’ to ‘%9’, but 10th and subsequent parameters cannot be used without doing anything. So by using Shift command once, the parameters will be shifted and 10th parameter will go into ‘%9’. You can use all parameters by repeating Shift.

Once shifted, the value of ‘%0’ is overwritten to the original value of ‘%1’, so the original value of ‘%0’ will be lost. If you want to use the original value of ‘%0’, you need to save the value to another environment variable or etc. in advance. (You cannot shift in reverse order.) [Windows NT series] [Extensions] By specifying ‘/1’, you can keep the value of ‘%0’.

Samples

Sample (Batch file)

@echo off
set MYTEMP=
:check
if "%1"=="" goto final
set MYTEMP=%MYTEMP%%1,
shift
goto check
:final
echo %MYTEMP%
set MYTEMP=

This batch program concatenates parameters specified to the program with ‘,’ and sets the result to the environment variable ‘MYTEMP’. When finishing concatenation, the program displays the value of ‘MYTEMP’ and removes ‘MYTEMP’ from the environment.