Skip to main content
typo-fix
Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 264

The problem is exactly what shelcheckshellcheck is telling you: for loops iterating over the output of find or similar commands are fragile. For example:

$ ls
'a file with spaces' 

$ for file in $(find . ); do    echo "$file"; done
.
./a
file
with
spaces

The safe way would be either to use the -exec of find:

$ find . -exec echo  {} \;
.
./a file with spaces

Or to use a while loop:

$ find . -print0 | while IFS= read -r -d '' file; do echo "$file"; done
.
./a file with spaces

The problem is exactly what shelcheck is telling you: for loops iterating over the output of find or similar commands are fragile. For example:

$ ls
'a file with spaces' 

$ for file in $(find . ); do    echo "$file"; done
.
./a
file
with
spaces

The safe way would be either to use the -exec of find:

$ find . -exec echo  {} \;
.
./a file with spaces

Or to use a while loop:

$ find . -print0 | while IFS= read -r -d '' file; do echo "$file"; done
.
./a file with spaces

The problem is exactly what shellcheck is telling you: for loops iterating over the output of find or similar commands are fragile. For example:

$ ls
'a file with spaces' 

$ for file in $(find . ); do    echo "$file"; done
.
./a
file
with
spaces

The safe way would be either to use the -exec of find:

$ find . -exec echo  {} \;
.
./a file with spaces

Or to use a while loop:

$ find . -print0 | while IFS= read -r -d '' file; do echo "$file"; done
.
./a file with spaces
Source Link
terdon
  • 252.2k
  • 69
  • 480
  • 718

The problem is exactly what shelcheck is telling you: for loops iterating over the output of find or similar commands are fragile. For example:

$ ls
'a file with spaces' 

$ for file in $(find . ); do    echo "$file"; done
.
./a
file
with
spaces

The safe way would be either to use the -exec of find:

$ find . -exec echo  {} \;
.
./a file with spaces

Or to use a while loop:

$ find . -print0 | while IFS= read -r -d '' file; do echo "$file"; done
.
./a file with spaces