Using the SHIFT Statement

Written by Barry Dysert (last updated December 13, 2021)

You may find the SHIFT statement useful in some of your batch files. What SHIFT does is shift the command-line parameters from right to left. (Command-line parameters are those parameters included on the command line used to execute your batch file.) This gives you one way, for example, to have your batch file accept a variable number of parameters.

Internally, the command interpreter numbers elements of the command line. For instance, consider the following command that executes a batch file named DOIT.BAT:

doit first second third fourth fifth

In this instance, the command is "doit", which is followed by five parameters: first, second, third, fourth, and fifth. The command interpreter, internally, numbers these parameters %1 through %5. In reality, though, there is a "parameter" numbered %0: the command (the batch file name) itself.

To see how this works, create your own DOIT.BAT file that contains the following commands:

@ECHO OFF
CLS
:TOP
IF [%0]==[] GOTO :EOF
ECHO %0 %1 %2 %3 %4 %5
SHIFT
GOTO TOP

The batch file first turns off echoing of commands to the screen, then clears the screen. It then sets up a loop of commands (from :TOP through the end of the file) that echoes, to the screen, the elements of the command line and then shifts (through use of the SHIFT command) all the elements to the left. After the shift, element %0 is discarded, element %1 becomes %0, element %2 becomes %1, and so on. When you run the batch file, you should see the following output:

doit first second third fourth fifth
first second third fourth fifth
second third fourth fifth
third fourth fifth
fourth fifth
fifth

When you use SHIFT in your own batch files, you can invoke the file with a variable number of parameters and then shift through those parameters to do your processing.

 This tip (13198) applies to Windows 7, 8, and 10.

Author Bio

Barry Dysert

Barry has been a computer professional for over 35 years, working in different positions such as technical team leader, project manager, and software developer. He is currently a software engineer with an emphasis on developing custom applications under Microsoft Windows. When not working with Windows or writing Tips, Barry is an amateur writer. His first non-fiction book is titled "A Chronological Commentary of Revelation." ...

MORE FROM BARRY

Changing Font Size in a Command Prompt Window

If you work at the command level very much, you may want to change the fonts that are used. You can control what ...

Discover More

Deleting a Saved Search

Windows Explorer has a good search utility built into it. As you use it, you may wish to delete a previously saved search ...

Discover More

Displaying the Home Button in Microsoft Edge

By default, Microsoft Edge does not display a Home button on its toolbar. If you like having the Home button visible, ...

Discover More
More WindowsTips

Using Batch Files, Part 1

This tip is part of a series that shows you how to create and use Windows batch files. It introduces a few commands and ...

Discover More

Using Batch Files, Part 3: The IF Command

This tip is part of a series that talks about Windows batch files. It introduces a few more commands you can use in your ...

Discover More

Running a Batch File at a Scheduled Time

Once you become comfortable with batch files, chances are that you'll want to use them to perform various system ...

Discover More
Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is five minus 3?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


Newest Tips