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
  • 175,680
  • 26
  • 273
  • 307
Fernando
  • 1,263
  • 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
  • 231,213
  • 25
  • 204
  • 289
4

Use an array:

A=("a*" "b*")
for ex in "${A[@]}"; do
    echo "$ex"
done
chepner
  • 497,756
  • 71
  • 530
  • 681