Wow; that sounds pretty exciting doesn’t it?
If you do much shell scripting (I frequent bash) at some point you’ll come across “IFS” and learn to appreciate the usefulness in manipulating it. Let’s look at a script showing that — and then dive into the details.
#!/usr/bin/env bash
## test function
ifs_test()
{
cd ~/tmp || exit 1
for entry in `ls -lah`
do
echo $entry
done
}
## let’s see how it behaves
ifs_test
## let’s modify IFS and run it again
oIFS=$IFS
IFS="
"
ifs_test
## let’s be anal and restore IFS explicitly
IFS=$oIFS
exit
So what’s happening here and how is it relevant?
A number of times I’ve had to write scripts to automate the processing of files, which can turn out to be any combination of renaming them, moving them around, or checking if they are valid based on some set of rules. One thing that can often bite me is file names with unexpected spaces. You may say, “but people know how to name the files correctly, there’s documentation for it even.” I’m sure you’re right, but you’re not accounting for the fact that these people are human. Humans make mistakes. You can get clever and pass results through another Unix tool, but I prefer to avoid extra dependencies when the shell I’m working in can handle it cleanly and efficiently.