I'm using the following statement in a bash script to iterate through the results of find:

find ./ -type f \( -iname \*.mkv -o -iname \*.mp4 \) | while read line; do

Now the problem is I can't ask for user input using "read" in this loop as it will give me the next line from the the find results.

read mainmenuinput

will give me the next filename instead of waiting for user input.

How can I work around this?

Thanks, rayfun

  • Is the input required for every file or just once at the beginning? Also be careful regarding spaces (or other "odd" characters) in filenames - it will break your read.
    – FelixJN
    Nov 14, 2019 at 10:38

1 Answer 1


You can use a different file descriptor for the find results, if you use a redirection from a process substitution into the loop:

# this reads from fd 3
while IFS= read -r line <&3; do
    # this reads from fd 0 (stdin)
    read -p "Enter your input" main_menu_input
    # .. do stuff
done 3< <(
    find ... 

This harms readability a bit as the find command shows up at the bottom, but it gives you the maximum flexibility wrt file descriptors. Additionally, the while loop is not run in a subshell due to piping, so if you're setting variables in the while loop that you rely on in code following the loop, you're now OK.

  • How would I apply your answer to this? Still learning shell scripting :-/ find ./ -type f \( -iname \*.mkv -o -iname \*.mp4 \) | while read line; do read mainmenuinput done
    – Rayfun
    Nov 13, 2019 at 19:08
  • That's exactly what I demonstrated. Nov 13, 2019 at 19:28
  • In my scenario I have a function called "pause", that will read input from the user asking whether to continue. I'm calling that function from multiple places within my find result processing. So I'd like to read input multiple times within that find | while; do construct. Any idea how to achieve that? Sorry I didn't explain in more detail. Thank you!
    – Rayfun
    Nov 13, 2019 at 19:33
  • I truly don't understand what you're asking me. It seems like you're asking your question to me over and over. I've answered it. Do you have any specific questions about my answer? Nov 14, 2019 at 3:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .