Loop over array, preventing wildcard expansion (*)

Your problem is that you want an array, but you wrote a single string that contains the elements with spaces between them. Use an array instead.

WHITELIST_DOMAINS=('*' '*.foo.com' '*.bar.com')

Always use double quotes around variable substitutions (i.e. "$foo"), otherwise the shell splits the the value of the variable into separate words and treats each word as a filename wildcard pattern. The same goes for command substitution: "$(somecommand)". For an array variable, use "${array[@]}" to expand to the list of the elements of the array.

for domain in "${WHITELIST_DOMAINS[@]}"
 do
    echo "$domain"
 done

For more information, see the bash FAQ about arrays.

Leave a Comment