Shellcheck doesn't like my for over find loop in Bash.
for f in $(find $src -maxdepth 1 -name '*.md'); do wc -w < "$f" >> $path/tmp.txt; done
It suggests instead:
1 while IFS= read -r -d '' file
2 do
3 let count++
4 echo "Playing file no. $count"
5 play "$file"
6 done < <(find mydir -mtime -7 -name '*.mp3' -print0)
7 echo "Played $count files"
I understand most of it, but some things are still unclear.
In line one: What is '' file?
In line six: What does the empty space do in < < (find). Are the < redirects, as usual? If they are, what does it mean to redirect into do block?
Can someone help parse this out? Is this the right way to iterate over files of a certain kind in a directory?
According to
help read, that''is an argument to the-dparameter:There are two separate operators there. There is
<, the standard I/O redirection operator, followed by a<(...)construct, which is a bash-specific construct that performs process substitution:So this is is sending the output of the
findcommand into thedoloop.Redirect into a loop means that any command inside that loop that reads from
stdinwill read from the redirected input source. As a side effect, everything inside that loop runs in a subshell, which has implications with respect to variable scope: variables set inside the loop won't be visible outside the loop.For the record, I would typically do this by piping
findtoxargs, although which solution is best depends to a certain extend on what you're trying to do. The two examples in your question do completely different things, and it's not clear what you're actually trying to accomplish.But for example:
This would run
wcon all the*.mdfiles. The-print0tofind(and the-0toxargs) permit this command to correctly handle filenames with embedded whitespace (e.g.,This is my file.md). If you know you don't have any of those, you just do: