Since I tend to forget this:
- you can declare bash functions with parenthesis
- you pass parameters to functions space delimited (often people quote each parameter)
- within functions you can refer to parameters by number just like the parameters passed to a shell script
More info at:
- [WayBack] Passing parameters to a Bash function – Stack Overflow
- [WayBack] Complex Functions and Function Complexities
I think for functions, you can apply what is linked from Some useful links on bash parameters: $1, $*, $@, quotes, etc., so a loop over all parameters in a function is the same as in a script, see [WayBack] shell – how to loop through arguments in a bash script – Unix & Linux Stack Exchange from Gilles:
There’s a special syntax for this:
for i do echo "$i" doneMore generally, the list of parameters of the current script or function is available through the special variable
$@.for i in "$@"; do echo "$i" doneNote 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





