Shell Scripting / Bash Tricks
Stop bash from choking on whitespace in your file paths
đź§© The Challenge
Everyone has had that moment where a script works perfectly until a file shows up with a space in its name and everything blows up. I spent an entire afternoon debugging a loop that thought a filename was three separate files, and trust me, that’s not a fun way to spend a Tuesday.
đź’ˇ The Fix
The fix is to use a null-terminated stream instead of relying on standard line breaks. It’s the only way to treat a filename as a single atomic unit, regardless of the junk people name their files.
find . -name "*.txt" -print0 | while IFS= read -r -d '' file; do
echo "Processing $file"
done
⚙️ Why It Works
Setting IFS to an empty string and using -d ” tells read to ignore standard delimiters and only stop when it hits a null byte, which is the only character you can’t put in a Linux filename. Using the print0 flag on find ensures you’re piping exactly those null bytes down the line.
🚀 Pro-Tip: Always wrap your variables in double quotes, but for filenames, the null-byte approach is the real industry standard.
Linux Tips & Tricks | © ngelinux.com | 8/11/2026
