[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Builtin read with -n0 or -N0 (nchars == 0) behaves as a read with no -n/

From: Eduardo Bustamante
Subject: Builtin read with -n0 or -N0 (nchars == 0) behaves as a read with no -n/-N argument
Date: Tue, 23 May 2017 10:33:17 -0500

(I think this is a good problem for Pranav to tackle if you consider
this to be a bug, Chet).

Current behavior:

address@hidden:~$ bash -c 'read -n0 <<< "abc"; declare -p REPLY'
declare -- REPLY="abc"

address@hidden:~$ bash --version|head -n1
GNU bash, version 4.4.11(1)-release (x86_64-pc-linux-gnu)

Expected behavior:

#1 nchars <= 0 treated as an error


address@hidden:~/src/gnu/bash-build$ ./bash -c 'read -n0 <<< "abc";
declare -p REPLY'
./bash: line 0: read: 0: invalid number
./bash: line 0: declare: REPLY: not found

#2 adopt the same semantics as POSIX read(2) syscall

http://pubs.opengroup.org/onlinepubs/9699919799/functions/read.html describes:

| Before any action described below is taken, and if nbyte is zero, the read()
| function may detect and return errors as described below. In the absence of
| errors, or if error detection is not performed, the read() function shall
| return zero and have no other results.

This is what the rest of the shells that support a similar option do:

address@hidden:~$ for sh in bash ksh93 mksh zsh; do echo $sh $($sh -c
'set -- -n 0; [ -n "$ZSH_VERSION" ] && set -k 0; read "$@" REPLY <<<
abc; typeset -p REPLY'); done
bash declare -- REPLY="abc"
ksh93 REPLY=''
mksh typeset REPLY=
zsh typeset REPLY=''

I'm in favour of #2.

This was reported by Freenode user `mknod' in #bash

reply via email to

[Prev in Thread] Current Thread [Next in Thread]