The Stack Overflow podcast is back! Listen to an interview with our new CEO.
2 of 2 added 161 characters in body

Why isn't it possible to read from `stdin` with `read` when piping a script to bash?

I'm not looking for work-arounds or solutions for the issue. I'm fine with it not working like that in bash. I just don't understand why it doesn't work.

I'm looking for an in-depth answer why the following script doesn't work. All previous internet search results, including posts from, couldn't really clear this up completely. It has something to do with read reading from stdin which doesn't work because stdin is already "taken" (?) by cat feeding bash via the pipe?

Example bash script

echo "Please say name:"
read NAME
echo "Hello $NAME"

Method 1 calling the script with bash

$ bash
Please say name:
Hello XYZ

Method 2 running the script via piping to bash:

$ cat | bash
Please say name:

So the script immediately returns to the prompt, without waiting for input or even printing the second line.