Bash function with arguments

argumentsbashfunction

I know I can write bash scripts like:

foo() {
  echo $1;
}

but can I define a function that writes:

foo(string) {
  echo $string;
}

I just can't find my way out of this.

Best Answer

The only available form is the first one; see the manual for details.

To use named parameters, the traditional technique is to assign them at the start of your function:

foo() {
  string=$1
  # ...
  echo "${string}"
}
Related Question