Another approach to process the passed arguments is to use "shift". Using the above example:
$1 will contain "King Alfred the Great"
$2 holds the number "1"
$3 holds the number "2"
The special variable "$#" contains the number of arguments.
The situation after a
shift:
$1 holds the number "1"
$2 holds the number "2"
and $# will be decreased by one to reflect the number of the remaining parameters.
A script to demonstrate that we can keep printing the first parameter in "$1" when we use
shift:
Code:
#!/bin/sh
# J65nko - daemonforums.org
while [ $# -ne 0 ]; do
nr=$((nr+1))
echo "Parameter $nr :\t$1"
shift
done
Running with same arguments as in the previous example:
Code:
./paramshift.sh "King Alfred the Great" 1 2
Parameter 1 : King Alfred the Great
Parameter 2 : 1
Parameter 3 : 2