"$@" means list of all supplied parameters
that code block is supposed to recognize options and set things up. For loop iterates over the param list and case tests current option against --color, -<anything> and <anything> and does things.
Code:
#!/bin/bash
for option in "$@"
do
case $option in
--color ) echo "'$option' matches --color";;
-* ) echo "'$option' matches -*";;
* ) echo "'$option' matches *";;
esac
done
test:
Code:
./args.sh --color --color-range -xyz 1 -t 2
'--color' matches --color
'--color-range' matches -*
'-xyz' matches -*
'1' matches *
'-t' matches -*
'2' matches *
What i find strange is the existence of shift there. I don't think it does anything in case of for loop.
Shift removes $1 from param list and other params move to the front ($2 becomes $1). It is usually used with while loop, you test against non-empty list, analyze $1 and then shift.
Code:
while (( $# ))
do
echo "$# parameter(s) remaining"
case $1 in
--color ) echo "'$1' matches --color";;
-* ) echo "'$1' matches -*";;
* ) echo "'$1' matches *";;
esac
shift
done
Code:
./args2.sh --color --color-range -xyz '--color aaa' -t 2
6 parameter(s) remaining
'--color' matches --color
5 parameter(s) remaining
'--color-range' matches -*
4 parameter(s) remaining
'-xyz' matches -*
3 parameter(s) remaining
'--color aaa' matches -*
2 parameter(s) remaining
'-t' matches -*
1 parameter(s) remaining
'2' matches *
while seems more powerful. Let's say you use some option with -switch value combo. You can conveniently take care of (-switch) and following (value) at once which would be more complicated in for loop
Code:
-switch ) option_a=on; shift; option_a_param=$1;