Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Help find Zsh' Bash `read -N1 -p "Put here " var
2023-08-22 19:23:23 +0700, Budi:
> What is the Zsh equivalent for Bash: read -N1 -p "Put here " var ?
[...]
read -k 'var?Put here '
Would be the closest equivalent (note that zsh's read has had -k
since 1994, while bash added its -N in 2010, copied from ksh
where it was added in 2003).
The read 'var?prompt' is from ksh (circa 1983) also in zsh from
the first version from 1990, bash's -p is from 1996.
The differences (that I can think of):
- in bash, read -N1 still does backslash processing even though
there's nothing to escape as IFS handling is not done and
newline is not treated specially, so if you enter \, you still
need to press another key. zsh's read -k reads one character.
Chances are you'd want to add the -r option in bash to avoid
that and align with the zsh behaviour.
- in bash NUL characters are ignored, so you can enter as many
^@s as you want, read won't return. zsh will return upon ^@
and store a NUL in $var.
- read -k reads from the terminal device regardless of where stdin
comes from, and puts that device in a mode where it sends
characters as soon as they are typed (and restore the original
settings upon return); bash's read -N1, like ksh's reads from
stdin, and if stdin is a terminal device, puts it in that mode
above. You'd do read -u0 -k in zsh to read one character from
stdin instead of the terminal, but then if stdin is a
terminal, it doesn't change its settings which means you'd
likely need to press Return for the terminal device to make
anything available for "read" to read..
In any case, both read a *character*, not necessarily a *byte*
and despite the "k", not necessarily all the characters that are
sent upon a key or key combination.
For instance
- pressing Shift+a will send a "A" which is both one byte and
one character.
- pressing € will send one € character which in UTF-8 locales is
made of 3 bytes
- pressing the "Home" key will generally send a sequence of
characters each generally made of one byte each, potentially
varying between terminals. For instance, on the terminal I'm
typing this on, ESC, [, 1 and ~ are sent, and you'd need 4
read -k or read -N1, or read -k4 or read -N4 to consume it.
To read one byte, you'd use:
LC_ALL=C read -u0 -k1 var # zsh
LC_ALL=C read -rN1 var # bash
with the caveat about NUL above. Actually, in bash, you'd
probably be better of using:
LC_ALL=C read -rd '' -n1 var
That is read up to one byte from a NUL-delimited record. So read
would return immediately upon the first byte received even if
it's 0. Beware of the effect on the exit status though.
--
Stephane
Messages sorted by:
Reverse Date,
Date,
Thread,
Author