Using the SHIFT Statement

by Barry Dysert
(last updated August 18, 2014)


You may find the SHIFT statement useful in some of your batch files. What SHIFT does is shift the parameters passed to the batch file from right to left. 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 name DOIT.BAT:

doit first second third fourth fifth

In this instance, the command provides five parameters to the DOIT.BAT file. The command interpreter, internally, numbers these parameters %1 through %5. In reality, though, there is a "parameter" numbered %0: the batch file name itself. Create your own DOIT.BAT file that contains the following:

IF [%0]==[] GOTO :EOF
ECHO %0 %1 %2 %3 %4 %5

All the batch file does is to echo, to the screen, the elements of the command line and then shifts (through 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

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

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

Author Bio

Barry Dysert

Barry has been a computer professional for over 30 years, working in different positions such as technical team leader, project manager, and software developer.  He is currently a senior software engineer with an emphasis on developing custom applications under Microsoft Windows. ...


Using Sticky Keys

Sticky Keys is an accessibility feature designed for people who have trouble holding down two or more keys simultaneously. ...

Discover More

Understanding the NTFS File System

The NTFS file system is at the core of the Windows operating system. This tip explains a bit about it.

Discover More

Renaming Your Computer

When you buy a new computer, it typically has some obscure name that doesn't accurately reflect your personality. Or perhaps ...

Discover More
More WindowsTips

PowerShell Input and Output

When dealing with a scripting language like PowerShell, one of the first things you need to learn is how to get data into and ...

Discover More

Passing Parameters to a PowerShell Script

Like the older batch-file processor, PowerShell can accept parameters. This allows for flexibility in your script. This tip ...

Discover More

Using Batch Files, Part 4: The CALL Statement

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

FREE SERVICE: Get tips like this every week in WindowsTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.


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}] 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 7 - 0?

2015-12-07 08:10:09

Thomas Redd

Thanks for the help you give! I am so excited about what I can now do with Batch Files!

2014-08-23 08:56:44


Sorry I wasn't as coherent as I should have been. Let me try simplifying it.

The SHIFT command causes the batch procedure to shift the command-line parameters one position to the left. So if you invoked a batch procedure called Barry.bat and specified parameters "one two three", i.e., you'd type (without quotes) "Barry.bat one two three", then inside Barry.bat the symbol %1 would take on the value of "one", the symbol %2 would take on the value of "two", and the symbol %3 would take on the value of "three". Each time the procedure does a SHIFT, the command-line parameters are shifted to the left. So after one SHIFT, the symbol %1 would take on the value of "two", and the symbol %2 would take on the value of "three". After another SHIFT, %1 would be "three"; and after another SHIFT, %1 would be empty.

Here's Barry.bat...
@echo off
echo %1 %2 %3
echo %1 %2
echo %1
echo %1

Here's the output when I invoke it with C:Bat>Barry.bat one two three
one two three
two three
ECHO is off.

I hope this makes more sense.

2014-08-22 17:02:35

Jibran Ahmed

Although, I appreciate your effort here Barry but indeed, I must have to agree with Angela that your article is far too technical to understand. Kindly, if it can be made a little easy for us the beginners? Thank you!

2014-08-18 19:10:29


Sorry but I didn't understand a word of this. far too technical.

Newest Tips

FREE SERVICE: Get tips like this every week in WindowsTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.