I have a shell script that processes some files. The problem is that there might be white spaces in file names, I did:
FILE=`echo $FILE | sed -e 's/[[:space:]]/\\ /g'`
echo $FILE | sed -e 's/[[:space:]]/\\ /g'
some terrible file.txt
some\ \ \ terrible\ \ file.txt
Don’t make it more complicated than it is.
That’s all you need. Note the quotes around the variable. They prevent the variable from being expanded and split at whitespace. You should always write your shell programs like that. Always put quotes around all your variables, unless you really want the shell to expand them.
for i in $pattern; do
That would be ok.