3

Always wondered this, but never fully investigated - is there any way to get named parameters in bash?

For example, I have this:

function ql_maybe_fail {
  if [[ "$1" == "true" ]]; then
      echo "quicklock: exiting with 1 since fail flag was set for your 'ql_release_lock' command. "
      exit 1;
  fi
}

is it somehow possible to convert it to something like this:

function ql_maybe_fail (isFail) {
  if [[ "$isFail" == "true" ]]; then
      echo "quicklock: exiting with 1 since fail flag was set for your 'ql_release_lock' command. "
      exit 1;
  fi
}

2 Answers2

6

Functions in Bash currently do not support user-named arguments.

fpmurphy
  • 4,636
4

This workaround might help, but it is not well testet:

fun () {
    v1=$1
    v2=$2
    for v in "$v1" "$v2"
    do
       case "$v" in
           name=*) name=${v/*=/};;
           age=*)  age=${v/*=/};;
           *)    echo "unexpected $v, please use name and age" ;;
       esac
    done

    echo "name=$name age=$age"
}

output:

fun "name=John" "age=22"
name=John age=22
fun "age=22" "name=John"
name=John age=22
user unknown
  • 10,482