preload
Mai 10
Situation

You want to operate on a linux, unix or mac system some files in a bash script and do some file operations like modifying the text content or moving the files to another location depending on the filename, content or another mathinc pattern.

Problem

Wondered why you have problems with windows files in a network when looping over them? Linux takes whitespaces in file name as line separator so that echoing the file will cause a separate line and so you cannot operate on this file.

Solution

I guess it is called Instruction Field Separator or something. You have to set this only to line break and not to whitespaces. First save the old variable content, then set the new content so that the bash do not takes whitespaces as line separators

1
2
3
4
5
6
7
8
9
OLDIFS=$IFS
IFS=$(echo -en "\n\b")
 
for FILE in $(find . -name '*.txt)
do
   echo $FILE
done
 
$IFS = $OLDIFS
Tagged with: