the codingmerc llc

application programming expert for hire

A shell loop over files with whitespace characters

is easy if you know this little trick. Usually, you could loop over files like this:

for f in *
do
echo "$f"
done

But the loop will cancel as soon as it tries the first file with a whitespace character. This is because the shell uses the whitespace character as a field separator. You can change the field separator however.

#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for f in *
do
echo "$f"
done
IFS=$SAVEIFS

Found it here: http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html 

© 2017 the codingmerc llc