The variable does contain the newlines; I suspect you’re leaving off the quotes somewhere important.
Femto% string=$'a\nstring\nwith\nnewlines'
Femto% echo $string
a
string
with
newlines
Femto% touch $string
Femto% touch "a string with spaces"
Femto% ls -l | cat
total 0
-rw-r--r-- 1 jtl staff 0 Feb 6 01:57 a
string
with
newlines
-rw-r--r-- 1 jtl staff 0 Feb 6 02:02 a string with spaces
We now have a file with newlines in its name, and one with spaces; the cat was required to keep ls from being helpful and filtering out unprintable characters.
Now, we try the original suggestion:
Femto% find . -name '*' | while read FILE ; do echo $FILE rocks.; done
. rocks.
./a rocks.
string rocks.
with rocks.
newlines rocks.
./a string with spaces rocks.
Well, that worked for files with spaces, but not with newlines, so it’s still dangerous.
Now, we try the simplest possible method, but with proper quoting, as Eridius suggested.
Femto% for FILE in * ; do echo "$FILE" rocks.; done
a
string
with
newlines rocks.
a string with spaces rocks.
The variable does contain the newlines; I suspect you’re leaving off the quotes somewhere important.
We now have a file with newlines in its name, and one with spaces; the cat was required to keep ls from being helpful and filtering out unprintable characters.
Now, we try the original suggestion:
Well, that worked for files with spaces, but not with newlines, so it’s still dangerous.
Now, we try the simplest possible method, but with proper quoting, as Eridius suggested.
Hey, that worked perfectly.