How to persist variables in a loop from a find command?
The usual way is to use a pipe:
There are two problems here. First, this won’t work if paths have spaces or other blank characters in them. Second, bash actually forks a sub-process when using the pipe, so everything that is done within the “while” loop is executed in a sub-process. If variables are set in this loop, they won’t be available outside the loop.
Here is a better way:
-print0 option for
find will separate entries using the
null character instead of the newline character, thus allowing spaces
and other blanks in path names. Using redirection in the example above
find in a subshell, rather than the loop itself, thus
preserving variables after the loop.