Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: minor 'select' snag

Op 15-02-16 om 12:48 schreef Peter Stephenson:
> On Mon, 15 Feb 2016 00:05:06 +0100
> Martijn Dekker <martijn@xxxxxxxx> wrote:
>> I'm analysing the behaviour of 'select' in various shells and found a a
>> way in which it's different in zsh from bash, ksh93 and {pd,m}ksh.
>> If a user presses Ctrl-D (EOF) within a 'select' loop, the REPLY
>> variable is left unchanged on zsh. On the other shells with 'select', it
>> is cleared, which is the same behaviour as 'read' (including 'read' on
>> zsh) and seems more logical. This makes it possible to decide whether to
>> continue after the loop by testing for the emptiness of $REPLY without
>> having to initialise it before entering the loop. It would be nice if
>> this worked the same way on zsh.
> This looks easy.  I don't think there's any existing moral right to
> expect REPLY to be unaffected in such circumstances.

This is from February and no action seems to have been taken since. I
spent some time looking into it now. Please consider the simple patch
attached. It makes zsh 'select' act exactly like bash and *ksh: if user
presses Ctrl-D (EOF), the REPLY variable is cleared, but if there is an
interrupt or error (e.g. Ctrl+C) the REPLY variable remains unaffected.

To reiterate, this makes it possible to check for EOF by checking the
emptiness of REPLY without having to manually initialise REPLY first,
like on bash, ksh93 and mksh.


- Martijn

diff --git a/Src/loop.c b/Src/loop.c
index fa7602e..3b9d021 100644
--- a/Src/loop.c
+++ b/Src/loop.c
@@ -289,6 +289,8 @@ execselect(Estate state, UNUSED(int do_exec))
 	    } else
 		str = (char *)getlinknode(bufstack);
+            if (!str && !errflag)
+                setsparam("REPLY", ztrdup("")); /* EOF (user pressed Ctrl+D) */
 	    if (!str || errflag) {
 		if (breaks)

Messages sorted by: Reverse Date, Date, Thread, Author