find | while read

Michael Torrie torriem at
Sat Apr 12 11:33:32 MDT 2008

Stuart Jansen wrote:
> Ever since I was taught "while read" as a method to avoid whitespace
> problems, it has been one of my favorite shell scripting tricks.
> Unfortunately, while working on a git script yesterday, I discovered an
> instance where it doesn't work. Specifically, Bash is leaking the output
> of find to the screen and horking stdin.

I have always assumed it was something to do with find closing it's
stdout before the while loop could read it all in.  But whatever the
case, I've not found any way around this, other than the workarounds
you've mentioned.

This problem has bitten me enough that I've started to use python for
shell scripting more and more.  I wrote a simple function to run a
command and return the errcode, stdout, and stderr.  Since most of the
times I use a pipe in bash is to do string manipulation on output,
having this simple run function is sufficient.  If I needed to actually
manipulate pipes in a dynamic way, I'd need something more.

More information about the PLUG mailing list