A Bash ‘for’ loop to iterate over a file that has blank spaces in its lines

I just had to write a Linux bash shell script that has a for loop that reads a file, where that file contains lines with blank spaces in it. This sounds simple, but blank spaces cause major problems in a Bash for loop.

Fortunately there’s a simple solution: Before the for loop, declare the input field separator to be a newline character, as shown in this for loop code:

local filecount=1
for i in `cat $FILES_FOUND`
    if [ $filecount -eq $fileNumber ]; then
        filename=`echo $i | cut -d: -f1`
        exploreFile $filename
    (( filecount++ ))

In this code, the variable FILES_FOUND is the name of a file that contains a list of strings, and those strings can contain blank spaces anywhere in each line.

As an example of how this works, imagine that the file contains ten records, and each record has ten blank spaces. When I use the IFS as shown, the for loop properly iterates ten times, once for each record. But if I had not used the IFS declaration, the for loop would have executed 100 times (10 times 10). That little IFS setting is huge when looping over lines in a file that have blank spaces.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.