0

I write a bash script and i have an array of function names with variable passed to them and i want to execute all of them in a loop.

but when i execute the bash script, i get this error:

a: command not found

how can i do this?

my bash script is look like this:

#!/bin/bash

functions_array=("test a" "test b" "testc")

test() {
        echo $1
}

testc() { echo "testc!"; }

for i in ${functions_array[@]}; do
        ${i}
done

1 Answer 1

1

You get this error because you didn't quote your variables. Therefore test a is split into two parts.

Try it like this:

#!/bin/bash

functions_array=("test a" "test b" "testc")

test() {
        echo "$1"                          # quoting here and ...
}

testc() { echo "testc!"; }

for i in "${functions_array[@]}"; do       # also here
        ${i}
done
Sign up to request clarification or add additional context in comments.

2 Comments

quotes is necessary just in for loop declaration.
Quotes are always a good idea. You can check your shell scripts here: shellcheck.net There it recommends the same.

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.