Advanced for loops

In the previous examples, we used the for loop to iterate over simple values where each value has no space.

As you know, if your values contain a space, you should use double quotes:

#!/bin/bash 
for var in one "This is two" "Now three" "We'll check four" 
do 
echo "Value: $var" 
done 

As you can see, each value is printed as expected thanks to double quotes.

This example contains values in one line and we quote the values because they have spaces and commas. What if the values were on multiple lines, as in a file?

What if the separator between the values we want to iterate over is something other than a space such as a comma ...

Get Mastering Linux Shell Scripting now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.