What's the zsh way to read n characters from stdin?
It's a bit weird, but it is documented:
-k
[num](…) Input is read from the terminal unless one of
-u
or-p
is present.
The reason your first attempt hangs there is that it's reading from the terminal. Typing three characters on the terminal does unblock it. To read from standard input when you're asking for a limited number of characters rather than a whole line (with -k
or -q
), you need to pass -u 0
explicitly.
echo foobar | ( read -u 0 -k 3; echo $REPLY )