Home > Software design >  Pass function name as argument when running bash script
Pass function name as argument when running bash script

Time:10-14

Assuming I have the following script:

#!/bin/bash

function hello (){
    echo hello,
}

function world (){
    echo world!
}

Is it possible to select the functions to run while I start the script? For exmaple:

./test.sh hello world

output:

hello,world!

CodePudding user response:

Just iterate over the arguments and run them.

for i in "$@"; do
    "$i"
done

Do not use function name(), just name(). See https://wiki.bash-hackers.org/scripting/obsolete . Check your scripts with shellcheck .

  •  Tags:  
  • bash
  • Related