The following shell snippet should iterate over the given space delimited string:
files="one two three"; for f in "$files"; do; echo "◌ $f"; done
Expecting:
◌ one
◌ two
◌ three
Getting:
◌ one two three
If the value for $files would have been hardcoded, it works with files=(one two three), however, the files="one two three" command ends up with `files="one two three".
How can I fix this loop to properly iterate over the string?
CodePudding user response:
Why not use ls -1a and awk?
For example:
ls -1a | awk '{ print "◌ " $0 }'
That should give you the list of file in the cwd in the following format:
◌ one
◌ three
◌ two
Or, as suggested in the comment, just use the for loop.
for f in *; do echo "◌ $f"; done
CodePudding user response:
I went with the following solution:
files="one two three"
array=(`echo $files`)
for f in $array; do
echo "◌ $f"
done
where $files can be the output of for example ls or some arbitrary command that gives you back a space delimited string.
Essentially then I could also do the following:
files=(`echo $(ls)`)
for f in $files; do
echo "◌ $f"
done

