O'Reilly logo

Linux and Unix Shell Programming by David Tansley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required