Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write Bash function with named input parameters

I would like to write a Bash function that uses named input parameters instead of positional parameters (eg. ${0} or ${1}). Is this possible, and if so, how do I achieve this?


1 Answers

Just reassign the parameters to something more intuitive:

function test {
    local foo=$1
    local bar=$2
    local baz=$3

    local msg='Function got called with parameters %s, %s, and %s\n'
    printf "$msg" "$foo" "$bar" "$baz"
}

If you're looking for something to make calling the function more user-friendly, look into getopt.

like image 167
miken32 Avatar answered Jul 18 '26 09:07

miken32



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!