Friday, September 07, 2007

Traverse an Argument List using shift Command

You can move through an argument list using the shift command. Here's an example using an inline while loop and and an inline for loop.

# sh

The while loop
# set M Y S Y S A D B L O G
# while [ $# -gt 0 ]
> do
> echo $*
> shift
> done
M Y S Y S A D B L O G
Y S Y S A D B L O G
S Y S A D B L O G
Y S A D B L O G
S A D B L O G
A D B L O G
D B L O G
B L O G
L O G
O G
G

The for loop
# set M Y S Y S A D B L O G
# for i in $*
> do
> echo $*
> shift
> done
M Y S Y S A D B L O G
Y S Y S A D B L O G
S Y S A D B L O G
Y S A D B L O G
S A D B L O G
A D B L O G
D B L O G
B L O G
L O G
O G
G

1 comment:

Anonymous said...

this is good to understand shift