There’s a special syntax for this:
for i do
echo "$i"
done
More generally, the list of parameters of the current script or function is available through the special variable $@
.
for i in "$@"; do
echo "$i"
done
Note that you need the double quotes around $@
, otherwise the parameters undergo wildcard expansion and field splitting. "$@"
is magic: despite the double quotes, it expands into as many fields as there are parameters.
print_arguments () {
for i in "$@"; do echo "$i"; done
}
print_arguments 'hello world' '*' 'special !\characters' # prints 3 lines
print_arguments '' # prints one empty line
print_arguments # prints nothing
Leave a Reply