From: Stephane Chazelas <stephane@chazelas.org>
To: Budi <budikusasi@gmail.com>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: Help find Zsh' Bash `read -N1 -p "Put here " var
Date: Tue, 22 Aug 2023 18:42:02 +0100 [thread overview]
Message-ID: <20230822174202.adcxbpipov7vsmc2@chazelas.org> (raw)
In-Reply-To: <CAH0GyZB8iFL0yqbEVxEhe4qPN8hNPk9SxoPvd5X-_p_A+1nf7A@mail.gmail.com>
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
next prev parent reply other threads:[~2023-08-22 17:42 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-22 12:23 Budi
2023-08-22 12:44 ` Pier Paolo Grassi
2023-08-22 14:19 ` Budi
2023-08-22 14:30 ` Peter Stephenson
2023-08-22 15:21 ` zeurkous
2023-08-22 15:34 ` Ellenor Bjornsdottir
2023-08-22 15:39 ` zeurkous
2023-08-22 17:01 ` Andreas Kusalananda Kähäri
2023-08-22 17:42 ` Stephane Chazelas [this message]
2023-08-22 19:01 ` Bart Schaefer
2023-08-22 19:03 ` Stephane Chazelas
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230822174202.adcxbpipov7vsmc2@chazelas.org \
--to=stephane@chazelas.org \
--cc=budikusasi@gmail.com \
--cc=zsh-users@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).