Safe Filename Loops With Bash Arrays
Collect names in an array and iterate with quoted expansion.
Use files=(*.md) then for f in "${files[@]}"; do ...; done to handle names with spaces.
01Command
Command
$ files=(*.md); for f in "${files[@]}"; do echo "$f"; done02What it does
The pattern for f in *.md breaks on filenames containing spaces, while an array preserves each element exactly. This is the reliable way to process many files in scripts, and it is a common source of automation bugs that a GUI never has to face.
03Risk
!Warning: Always quote "${array[@]}"; unquoted expansion re-splits elements on whitespace.
04Undo
✓No undo needed — this command does not change persistent state.
05Sources
- bash.1.en.html manual pageEvidence: The bash manual page documents arrays.
Last verified: 2026-10-13 · Category: Developer