zsh-users
 help / color / mirror / code / Atom feed
* Help find Zsh' Bash `read -N1 -p "Put here " var
@ 2023-08-22 12:23 Budi
  2023-08-22 12:44 ` Pier Paolo Grassi
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Budi @ 2023-08-22 12:23 UTC (permalink / raw)
  To: Zsh Users

What is the Zsh equivalent for Bash: read -N1 -p "Put here " var ?

AI chat gave no solution


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Help find Zsh' Bash `read -N1 -p "Put here " var
  2023-08-22 12:23 Help find Zsh' Bash `read -N1 -p "Put here " var Budi
@ 2023-08-22 12:44 ` Pier Paolo Grassi
  2023-08-22 14:19   ` Budi
  2023-08-22 15:21 ` zeurkous
  2023-08-22 17:42 ` Stephane Chazelas
  2 siblings, 1 reply; 11+ messages in thread
From: Pier Paolo Grassi @ 2023-08-22 12:44 UTC (permalink / raw)
  To: Budi; +Cc: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 701 bytes --]

the manual offers:

  -k [ num ]
                     Read only one (or num) characters.  All are assigned
to the first name, without word splitting.  This flag is ignored when -q is
present.  Input
                     is read from the terminal unless one of -u or -p is
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
MULTIBYTE is set.

Pier Paolo Grassi


Il giorno mar 22 ago 2023 alle ore 14:23 Budi <budikusasi@gmail.com> ha
scritto:

> What is the Zsh equivalent for Bash: read -N1 -p "Put here " var ?
>
> AI chat gave no solution
>
>

[-- Attachment #2: Type: text/html, Size: 1287 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Help find Zsh' Bash `read -N1 -p "Put here " var
  2023-08-22 12:44 ` Pier Paolo Grassi
@ 2023-08-22 14:19   ` Budi
  2023-08-22 14:30     ` Peter Stephenson
  0 siblings, 1 reply; 11+ messages in thread
From: Budi @ 2023-08-22 14:19 UTC (permalink / raw)
  To: Pier Paolo Grassi; +Cc: Zsh Users

the ... -p "Put here " var

?

On 8/22/23, Pier Paolo Grassi <pierpaolog@gmail.com> wrote:
> the manual offers:
>
>   -k [ num ]
>                      Read only one (or num) characters.  All are assigned
> to the first name, without word splitting.  This flag is ignored when -q is
> present.  Input
>                      is read from the terminal unless one of -u or -p is
> 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
> MULTIBYTE is set.
>
> Pier Paolo Grassi
>
>
> Il giorno mar 22 ago 2023 alle ore 14:23 Budi <budikusasi@gmail.com> ha
> scritto:
>
>> What is the Zsh equivalent for Bash: read -N1 -p "Put here " var ?
>>
>> AI chat gave no solution
>>
>>
>


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Help find Zsh' Bash `read -N1 -p "Put here " var
  2023-08-22 14:19   ` Budi
@ 2023-08-22 14:30     ` Peter Stephenson
  0 siblings, 0 replies; 11+ messages in thread
From: Peter Stephenson @ 2023-08-22 14:30 UTC (permalink / raw)
  To: Zsh Users

> On 22/08/2023 15:19 Budi <budikusasi@gmail.com> wrote:
> the ... -p "Put here " var
> ?

read -k 1 REPLY'?Type a character: '

> On 8/22/23, Pier Paolo Grassi <pierpaolog@gmail.com> wrote:
> > the manual offers:
> >
> >   -k [ num ]
> >                      Read only one (or num) characters.  All are assigned
> > to the first name, without word splitting.  This flag is ignored when -q is
> > present.  Input
> >                      is read from the terminal unless one of -u or -p is
> > 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
> > MULTIBYTE is set.
> >
> > Pier Paolo Grassi
> >
> >
> > Il giorno mar 22 ago 2023 alle ore 14:23 Budi <budikusasi@gmail.com> ha
> > scritto:
> >
> >> What is the Zsh equivalent for Bash: read -N1 -p "Put here " var ?
> >>
> >> AI chat gave no solution
> >>
> >>
> >


^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: Help find Zsh' Bash `read -N1 -p "Put here " var
  2023-08-22 12:23 Help find Zsh' Bash `read -N1 -p "Put here " var Budi
  2023-08-22 12:44 ` Pier Paolo Grassi
@ 2023-08-22 15:21 ` zeurkous
  2023-08-22 15:34   ` Ellenor Bjornsdottir
  2023-08-22 17:42 ` Stephane Chazelas
  2 siblings, 1 reply; 11+ messages in thread
From: zeurkous @ 2023-08-22 15:21 UTC (permalink / raw)
  To: Budi, Zsh Users

On Tue, 22 Aug 2023 19:23:23 +0700, Budi <budikusasi@gmail.com> wrote:
> AI chat gave no solution

This has to be some kind of parody.

        --zeurkous.

-- 
Friggin' Machines!


^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: Help find Zsh' Bash `read -N1 -p "Put here " var
  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
  0 siblings, 2 replies; 11+ messages in thread
From: Ellenor Bjornsdottir @ 2023-08-22 15:34 UTC (permalink / raw)
  To: Budi, Zsh Users

The snarky comments aren't well appreciated, zeurkous. Please don't chastise people for thinking GPT can do things it can't.

OP, GPT can't help you understand or accomplish this task. It's just a language model and it doesn't actually understand anything. It's been missold as this super app that knows everything.

What are you trying to do? Are you trying to read a single character from std. input into ${var}? (I opened the Bash operator's handbook to page... this thing https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html and that looks to be what you were trying to do)

If I open `man zshbuiltins` and scroll down to the info for the `read` command I am told I can pass -k and a number to `read`. I think someone in this thread already mentioned that, without explaining how they got that.
-- 
Ellenor Bjornsdottir (she)
sysadmin umbrellix.net


^ permalink raw reply	[flat|nested] 11+ messages in thread

* RE: RE: Help find Zsh' Bash `read -N1 -p "Put here " var
  2023-08-22 15:34   ` Ellenor Bjornsdottir
@ 2023-08-22 15:39     ` zeurkous
  2023-08-22 17:01     ` Andreas Kusalananda Kähäri
  1 sibling, 0 replies; 11+ messages in thread
From: zeurkous @ 2023-08-22 15:39 UTC (permalink / raw)
  To: Ellenor Bjornsdottir; +Cc: Budi, Zsh Users

While me has no intention of holding a discussion about this off-topic
subject here, me'll at least take responsibility for me words. 

On Tue, 22 Aug 2023 15:34:18 +0000, Ellenor Bjornsdottir <ellenor@umbrellix.net> wrote:
> The snarky comments aren't well appreciated, zeurkous=2E

Me'd answer this w/ a "speak for yourself", *if* mewas being snarky.
Suffice it to say that's not the case -- it's the idea of consulting
some fancy ELIZA successor, instead of RTFMing, that makes me groan
(& wonder how much faster humanty's backslide will get).
 
> Please don't chast=
> ise people for thinking GPT can do things it can't=2E

Like AI (it's actually plain old boring ML, but that doesn't sound
fancy enough for the n00bs, so the peddlers lie). But like mesaid:
here, that's off-topic.

Baai,

        --zeurkous.
 
-- 
Friggin' Machines!


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Help find Zsh' Bash `read -N1 -p "Put here " var
  2023-08-22 15:34   ` Ellenor Bjornsdottir
  2023-08-22 15:39     ` zeurkous
@ 2023-08-22 17:01     ` Andreas Kusalananda Kähäri
  1 sibling, 0 replies; 11+ messages in thread
From: Andreas Kusalananda Kähäri @ 2023-08-22 17:01 UTC (permalink / raw)
  To: Ellenor Bjornsdottir; +Cc: Budi, Zsh Users

On Tue, Aug 22, 2023 at 03:34:18PM +0000, Ellenor Bjornsdottir wrote:
> The snarky comments aren't well appreciated, zeurkous. Please don't chastise people for thinking GPT can do things it can't.
> 
> OP, GPT can't help you understand or accomplish this task. It's just a language model and it doesn't actually understand anything. It's been missold as this super app that knows everything.
> 
> What are you trying to do? Are you trying to read a single character from std. input into ${var}? (I opened the Bash operator's handbook to page... this thing https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html and that looks to be what you were trying to do)
> 
> If I open `man zshbuiltins` and scroll down to the info for the `read` command I am told I can pass -k and a number to `read`. I think someone in this thread already mentioned that, without explaining how they got that.
> -- 
> Ellenor Bjornsdottir (she)
> sysadmin umbrellix.net

A little tip that may help with finding the documentation for zsh
built-ins:

	if ((${+aliases[run-help]})); then unalias run-help; fi
	autoload -Uz run-help

This will make the run-help command available, which will present the
manual page for the command that you give it as an argument (including
the correct documentation for zsh built-ins).  According to the run-help
source code: "Figure out where to get the best help, and get it."

-- 
Andreas (Kusalananda) Kähäri
SciLifeLab, NBIS, ICM
Uppsala University, Sweden

.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Help find Zsh' Bash `read -N1 -p "Put here " var
  2023-08-22 12:23 Help find Zsh' Bash `read -N1 -p "Put here " var Budi
  2023-08-22 12:44 ` Pier Paolo Grassi
  2023-08-22 15:21 ` zeurkous
@ 2023-08-22 17:42 ` Stephane Chazelas
  2023-08-22 19:01   ` Bart Schaefer
  2023-08-22 19:03   ` Stephane Chazelas
  2 siblings, 2 replies; 11+ messages in thread
From: Stephane Chazelas @ 2023-08-22 17:42 UTC (permalink / raw)
  To: Budi; +Cc: Zsh Users

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


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Help find Zsh' Bash `read -N1 -p "Put here " var
  2023-08-22 17:42 ` Stephane Chazelas
@ 2023-08-22 19:01   ` Bart Schaefer
  2023-08-22 19:03   ` Stephane Chazelas
  1 sibling, 0 replies; 11+ messages in thread
From: Bart Schaefer @ 2023-08-22 19:01 UTC (permalink / raw)
  To: Budi, Zsh Users

On Tue, Aug 22, 2023 at 10:42 AM Stephane Chazelas
<stephane@chazelas.org> wrote:
>
> - in bash, read -N1 still does backslash processing

Compare "read -n1" (which you did reference in your later examples).

> - read -k reads from the terminal device regardless of where stdin

This can be changed by e.g. "read -k 1 -u 0" (or other descriptor number).

> 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.

If one wants to read full key combinations from within a ZLE widget,
one should use "zle read-command" instead.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Help find Zsh' Bash `read -N1 -p "Put here " var
  2023-08-22 17:42 ` Stephane Chazelas
  2023-08-22 19:01   ` Bart Schaefer
@ 2023-08-22 19:03   ` Stephane Chazelas
  1 sibling, 0 replies; 11+ messages in thread
From: Stephane Chazelas @ 2023-08-22 19:03 UTC (permalink / raw)
  To: Budi, Zsh Users

2023-08-22 18:42:02 +0100, Stephane Chazelas:
[...]
> 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.
[...]

Sorry, should have been:

LC_ALL=C IFS= read -rd '' -n1 var

While bash's read doesn't do IFS processing with -N, it does
with -n, so you need to set IFS to the empty string for read
as otherwise it would strip any character in $IFS if input
before storing into $var.

May be worth mentioning the sysread builtin from the zsh/system
module which gives you more control when reading a fixed number
of bytes (see info zsh sysread).

-- 
Stephane


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2023-08-22 19:08 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-22 12:23 Help find Zsh' Bash `read -N1 -p "Put here " var 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
2023-08-22 19:01   ` Bart Schaefer
2023-08-22 19:03   ` Stephane Chazelas

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).