From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 29356 invoked from network); 22 Aug 2023 17:42:37 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 22 Aug 2023 17:42:37 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1692726157; b=BcyRwDENcM/rkkqH5rBmXRoDxcklSsc8qdSQJWdWkB/nbdD0OOYdw3wCbWnpYkaYe7UbyxyQ4G rZ4mNGeBrNRoPmaPRgWQJftgza49u4h9BI3cK5oYjz7hWye+lJ72rsOz2Nap7V+eNqJL/+/Sa4 MeOOEV499ur/HgdI+PqRNjVKaoK1QecG/BqAkoCCVCrdCvpJDJ65U4UMc56UZsE5Clsst6Va81 2JpaiVn3F2sEn8YOikn/h0DvgJoqG5dj+cdC64jfx3XHUP/DNN9Mec9mwk/GNjC2OY2AjsWNAK FN/8VLqzik9PBHGg9xLgdkenjaMcXJKiMbSDGRDQlsprCA==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (relay7-d.mail.gandi.net) smtp.remote-ip=217.70.183.200; dmarc=none header.from=chazelas.org; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1692726157; bh=qUgN8TNAmIuwMie8VEW+NBXAiyu78DMCXmS6TYYrn/4=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:In-Reply-To:Content-Transfer-Encoding:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:DKIM-Signature; b=PPPhgg3iZB5TPWpBQK2Gmy/veB3QZhOjHDphkII4erJv3JFfzVjpx26biwpJNe6BS8fbeL9zeO zqgXjmcwurGVk/tvzx0wrAhGU3KiK0Gc2mPB6jV4RvKblvJVbkeRs+ekw8ZYAyOuFibnxMydGR hBRgn3VPUn+DU3symlPI+uC+yfVssxDwkDaBKmAG2P1PX5wTMiJIBYyfzf2Qhdtk60reeHf9yq 8af19npe8QQgEXHtfYQ629kmKk0MvwRn3hUm9jjirgBN4Vl0QKEBl2pZCRHllJjfvj+NTAZIpO XtJEVYICKMBdvAykYSUGX7Fw0ckUdbI7jyKtgvexnbQ96g==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:In-Reply-To:Content-Transfer-Encoding :Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To:From:Date: Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender :Resent-To:Resent-Cc:Resent-Message-ID; bh=X1EEo738UVLxtiLUufHKWu+UIRs1E2TkcCfMGf9rrgI=; b=XUy9g6BBg+8lCp5GRkeNltxKHF X9HEPdgbofMBdwv+Za0TVfiPKZAhmL9sUq64r3wRtrVTv+yGeKYujJ9WdKZmZnfWYMpfwIJwEeCO1 umdmn1CyB+r1VPxLFqwvmw1NjvnPXK4BZWuAEkQsBz9y59+sMA9ZDfH2UFTQ/oAHHE8P3b+EPWM22 gX7WBpBFRG4umNIKniXPj/mxzwNj5w3XOmXbY9pAwmWl8OY77FLo6aoie5/OSFJj1QAHhNYepM6Qg kEFp3ScJzt9/TCURGVVG8aQBk6xphXGdiPeDkt4a+XH4BLehm0AE40iNBPKiell+TOhQoc2PYItEa VsebPyIA==; Received: by zero.zsh.org with local id 1qYVOn-000I16-CM; Tue, 22 Aug 2023 17:42:37 +0000 Authentication-Results: zsh.org; iprev=pass (relay7-d.mail.gandi.net) smtp.remote-ip=217.70.183.200; dmarc=none header.from=chazelas.org; arc=none Received: from relay7-d.mail.gandi.net ([217.70.183.200]:57461) by zero.zsh.org with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) id 1qYVOF-000HKK-SV; Tue, 22 Aug 2023 17:42:05 +0000 Received: by mail.gandi.net (Postfix) with ESMTPSA id E4DFF20003; Tue, 22 Aug 2023 17:42:02 +0000 (UTC) Date: Tue, 22 Aug 2023 18:42:02 +0100 From: Stephane Chazelas To: Budi Cc: Zsh Users Subject: Re: Help find Zsh' Bash `read -N1 -p "Put here " var Message-ID: <20230822174202.adcxbpipov7vsmc2@chazelas.org> Mail-Followup-To: Budi , Zsh Users References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-GND-Sasl: stephane@chazelas.org X-Seq: 29187 Archived-At: X-Loop: zsh-users@zsh.org Errors-To: zsh-users-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-users-request@zsh.org X-no-archive: yes List-Id: List-Help: , List-Subscribe: , List-Unsubscribe: , List-Post: List-Owner: List-Archive: 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