6

I'm trying to figure out what I thought would be a trivial issue in BASH, but I'm having difficulty finding the correct syntax. I want to loop over an array of values, one of them being an asterisk (*), I do not wish to have any wildcard expansion happening during the process.

 WHITELIST_DOMAINS="* *.foo.com *.bar.com"
 for domain in $WHITELIST_DOMAINS
 do
    echo "$domain"
 done

I have the above, and I'm trying to get the following output:

 *
 *.foo.com
 *.bar.com

Instead of the above, I get a directory listing on the current directory, followed by *.foo.com and *.bar.com

I know I need some escaping or quoting somewhere.. the early morning haze is still thick on my brain.

I've reviewed these questions:

How to escape wildcard expansion in a variable in bash?

Stop shell wildcard character expansion?

Community
  • 1
  • 1
Tal
  • 617
  • 1
  • 6
  • 16

2 Answers2

7

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.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
  • Palm firmly planted on face -- I should have known that I wasn't using an array, thank you for pointing this out so clearly -- and quickly. – Tal Nov 06 '12 at 14:08
  • @"otherwise the shell splits the the value of the variable into separate words and treats each word as a filename wildcard pattern" This aspect of shell scripting can be the most difficult to remember when moving between multiple languages. Thank you for refreshing my memory. – Tal Nov 06 '12 at 14:19
2

You can use array to store them:

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

for i in "${array[@]}"
do
    echo "$i"
done
kev
  • 155,172
  • 47
  • 273
  • 272
  • This is the same solution as the accepted answer, except that @Giles gave a bit more useful explanation for future users sauntering along via Google. I've accepted his comment over yours for completeness and posterity. – Tal Nov 06 '12 at 14:13