20.1. The shift command

When parameters are passed to a script a method is required that will help us shift through each parameter so that we can process the options. This is what the shift command does. It shifts positional arguments one place to the left. To explain how this works let’s first look at a simple script using a while loop to echo out all the arguments passed to a script.

						$ pg opt2 
#!/bin/sh 
# opt2 
loop=0 
while [ $# -ne 0 ]    # while there are still arguments 
do 
  echo $1 
done 

You may think that the script above will process until there are no more arguments left on the command line. Wrong, I’m afraid. Because there is no way to shift to the next parameter inside the script, it will just keep echoing out the first argument. Here’s ...

Get Linux and Unix Shell Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.