I've finally run into the surprising stdin behavior when piping into a while read loop.
Consider the following:
find . | while read file;
do
echo "==[$file]==";
cat;
done
In this instance, cat
is just a stand-in for any command that receives input from STDIN. It's surprising (to me at least) that cat
's STDIN is actually coming from find
, so it gobbles up the rest of the find
output.
Suppose one wanted to interact directly from the tty with the command in cat
's place. E.g. Suppose instead of cat
you wanted to run a script which might ask questions you wanted to respond to interactively ("<file> exists: Overwrite? [y/n]"
).
Is there a way to force the inner command's STDIN to be the tty?
I've found a lot of similar questions, including this: Why redirect stdin inside a while read loop in bash?
But I couldn't understand the answer well enough to get it to work.
(edit: in light of clarifications to that other question, I'm now considering this a duplicate of that question.)