Wi-Fizzle.com - Putting the fizzle in Wi-Fi since 2005 .. (yes, this was a poor choice for a domain name)

#276
BASH for-loop split on newline

Posted by dandriff on Sunday June 29, 2008@10:43AM

Howto: Coax a BASH shell script into splitting for-loop elements by newline instead of whitespace (whitespace is the default behaviour).

#!/bin/bash

IFS_BAK=$IFS
IFS="
"

for f in `ls -1 .`; do
  echo "I found a directory! => $d"
done

IFS=$IFS_BAK
IFS_BAK=

This is really being posted for future reference by me, as I seem to keep forgetting that the "magic" variable name is IFS and not EOL.

Keywords: IFS, EOL, end of line, for-loop, newline, end-of-line,