Shell Scripting / Bash Tricks
Stop wrestling with arrays that turn into space-separated messes
🧩 The Challenge
You have probably tried to pass a list of files to a function, only to have the shell mangle your filenames because they have spaces in them. It’s infuriating when a perfectly good script breaks just because a coworker named a directory with a space.
💡 The Fix
Use bash arrays properly instead of flat strings to keep your data structures intact. This way, the shell treats each element as a distinct entity no matter what crazy characters are lurking inside.
my_files=("file one.txt" "file two.txt" "another boring file.log")
for file in "${my_files[@]}"; do
process_file "$file"
done
⚙️ Why It Works
Quoting the array expansion with the at-symbol inside brackets ensures the shell preserves the boundaries of every individual element. If you leave those quotes off, the shell performs word splitting and your filenames will get chopped up into pieces, which is usually not what you want.
🚀 Pro-Tip: Always use the @ symbol inside those quotes, because * just merges everything into one giant headache.
Linux Tips & Tricks | © ngelinux.com | 11/13/2026
