3 ms·
The options are good, but I don't like the IFS suggestion. Bash has a perfectly good way of dealing with spaces in array elements. Fixing the example in the a
by jonahx 6y ago
The options are good, but I don't like the IFS suggestion. Bash has a perfectly good way of dealing with spaces in array elements. Fixing the example in the article is as simple as adding double quotes around the array:
for name in "${names[@]}"; do
echo "$name"
done
The will not result in a single element, but will destructure each element of names, as if each were double-quoted, and will print the same output that the "strict mode IFS setting" does.
What's more, the double-quoted array solution will work even if the elements contain newlines or tabs.