1

I want to use a list as parameter to a for-loop; but with double quotes, the list isn't understood as a list, and without quotes, the wildcards are evaluated to filenames.

A="a* b*"

for ex in "$A"; do
    echo "$ex";
done

for ex in $A; do
    echo "$ex";
done

The first one prints:

a* b*

The latter prints:

a.txt
b.txt

I want:

a*
b*

How can I make this work?

ruakh
  • 166,710
  • 24
  • 259
  • 294
Fernando
  • 1,253
  • 10
  • 11

2 Answers2

5

You can set -f to disable pathname expansion:

A="a* b*"

( set -f
  for ex in $A ; do
      echo "$ex"
  done
)
choroba
  • 216,930
  • 22
  • 195
  • 267
4

Use an array:

A=("a*" "b*")
for ex in "${A[@]}"; do
    echo "$ex"
done
chepner
  • 446,329
  • 63
  • 468
  • 610