Search code examples
posix

Iterate through list containing an asterisk in posix


list="1 2 * 4"
for item in $list; do
  echo "$item"
done

The echo call in the loop evaluates the asterisk to be all the files in the current dir but I need a loop that prints the asterisk character.

I can't use an array (e.g list=('1' '2' '*' '4') ) because the implementation needs to work with posix and can't use bash.


Solution

  • You need to turn off globbing with set -f:

    set -f
    for item in $list
    do
        echo "$item"
    done
    set +f