1

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!
0

2 Answers 2

5

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 .

Sign up to request clarification or add additional context in comments.

2 Comments

That about covers it all -- you left nothing to add...
Thank you very much.
-1

You can use eval command to execute first argument.

#!/bin/bash

function hello (){
    echo hello,
}

function world (){
    echo world!
}

eval $1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.