1 of 2

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

I'm looking for an in-depth answer why the following script doesn't work. All previous internet search results, including posts from unix.stackexchange.com, 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 test.sh:

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

Method 1 calling the script with bash test.sh:

$ bash test.sh
Please say name:
XYZ
Hello XYZ
$

Method 2 running the script via piping to bash:

$ cat test.sh | bash
Please say name:
$

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