In bash, if I wanted to read, say 3, characters from a pipe, I could do:
... | read -n3
read, the closest option seems to be
-k[ num ]
Read only one (or num) characters. All are assigned to the first name, without word splitting. This flag is ignored when
-qis present. Input is read from the terminal unless one of
-pis present. This option may also be used within zle widgets.
Note that despite the mnemonic ‘key’ this option does read full characters, which may consist of multiple bytes if the option
Input is read from file descriptor n.
Input is read from the coprocess.
echo foobar | (read -k3; echo $REPLY) hangs waiting for input.
-p fails with
read: -p: no coprocess. Only the following works:
echo foobar | (read -k3 -u0; echo $REPLY)
This is the first time I'm seeing something that's more difficult to achieve in zsh than in bash.
Is there a simpler way to read N characters from stdin (whatever that might be) than this?