Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #11486

Re: `foo=1 declare -r foo' prints an error message but its exit status is zero

Path csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!usenet.stanford.edu!not-for-mail
From Dan Douglas <d@ndougl.as>
Newsgroups gnu.bash.bug
Subject Re: `foo=1 declare -r foo' prints an error message but its exit status is zero
Date Fri, 11 Sep 2015 02:04:07 -0500
Lines 94
Approved bug-bash@gnu.org
Message-ID <mailman.901.1441955106.19560.bug-bash@gnu.org> (permalink)
References <D447E11E-FFFF-4AE2-B737-1885D6C4FE75@qq.com>
NNTP-Posting-Host lists.gnu.org
Mime-Version 1.0
Content-Type multipart/signed; boundary="nextPart1631624.KXbFBnMrbe"; micalg="pgp-sha1"; protocol="application/pgp-signature"
X-Trace usenet.stanford.edu 1441955106 10803 208.118.235.17 (11 Sep 2015 07:05:06 GMT)
X-Complaints-To action@cs.stanford.edu
Cc ziyunfei <446240525@qq.com>
To bug-bash@gnu.org
Envelope-to bug-bash@gnu.org
DKIM-Signature v=1; a=rsa-sha1; c=relaxed; d=ndougl.as; h=from:to:cc:subject:in-reply-to:references:mime-version:content-type; s=s1; bh=Kcu8l3qycLW/7nKrNhtmwU2m8Og=; b=Y1eodXojpK1inP+HnH+lWp3 4L6VS1ECiNPg/fBE6wC2RzfHy1vwcU3t2MHAPAFclbEQ4Uu4ZyMN3YrZMytuIDVy k+iL6lejhwRWFYtnEcrLTkGth7P3Cs69xKU5uLTletiyBCf+SksdiwYOcNs+zJwN GjoacsLenOMNJIXN+LEc=
User-Agent KMail/5.0.43 pre (Linux/4.2.0; KDE/5.14.0; x86_64; ; )
In-Reply-To <D447E11E-FFFF-4AE2-B737-1885D6C4FE75@qq.com>
X-SG-EID 0BFV9TQLtfCCO+MFdgK90pmh300FCxFSbG//gVweYMnE0WRRKLX25DZu6lRzJyx/fulVLL7JWDtp84 fM2r1nfO8M95WmNavJitoiIu6AwGJqMy0u5W0zUM7NbMvL6nk+HS+FIZaTxB57WpfNFcRtTSL/L5oT 3hpyQDiWZJuewMA=
X-SendGrid-Contentd-ID {"test_id":"1441955060"}
X-detected-operating-system by eggs.gnu.org: GNU/Linux 3.x
X-Received-From 50.31.49.42
X-BeenThere bug-bash@gnu.org
X-Mailman-Version 2.1.14
Precedence list
List-Id Bug reports for the GNU Bourne Again SHell <bug-bash.gnu.org>
List-Unsubscribe <https://lists.gnu.org/mailman/options/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=unsubscribe>
List-Archive <http://lists.gnu.org/archive/html/bug-bash>
List-Post <mailto:bug-bash@gnu.org>
List-Help <mailto:bug-bash-request@gnu.org?subject=help>
List-Subscribe <https://lists.gnu.org/mailman/listinfo/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=subscribe>
Xref csiph.com gnu.bash.bug:11486

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On Wednesday, September 9, 2015 2:17:30 PM CDT ziyunfei wrote:
> $ foo=1 declare -r foo
> bash: foo: readonly variable
> $ echo $?
> 0
> $ echo $foo
> 1
> 
> Is this a bug? 
> 
> $ bar=1 readonly bar; # throw an error in Bash 4.2, fixed in Bash 4.3

It's a bit more illustrative when you use `readonly' instead of `declare'. In
the former case bash only prints the error in non-POSIX mode because it
modifies the special builtin to unset its value when the command finishes.
`declare' will print an error in either mode. If there's any bug here it's that
bash fails to bail out and executes the echo command anyway.

 $ ( for sh in bash sh ksh mksh zsh dash bb posh; do printf '%-5s x=%5s status=%s\n' "${sh}:" "$("$sh" -c "\${ZSH_VERSION+false}||emulate sh; trap 'echo \${x+unset}' EXIT; $(</dev/fd/3)")" "$?"; done ) 3<<\EOF
x=1 readonly x=2
EOF

bash: x=unset status=0
sh: x: readonly variable
sh:   x=unset status=0
ksh:  x=unset status=0
mksh: x=unset status=0
zsh:  x=unset status=0
dash: x=unset status=0
bb:   x=unset status=0
posh: x=unset status=0

It's possible to trigger a similar error in almost any shell by forcing
readonly to modify the value after the attribute is set.

 $ ( for sh in bash sh ksh mksh zsh dash bb posh; do printf '%-5s x=%5s status=%s\n' "${sh}:" "$("$sh" -c "\${ZSH_VERSION+false}||emulate sh; trap 'echo \${x+unset}' EXIT; $(</dev/fd/3)")" "$?"; done ) 3<<\EOF
readonly x=1 "x=2"
EOF

bash: x: readonly variable
bash: x=unset status=1
sh: x: readonly variable
sh:   x=unset status=1
ksh: readonly: x: is read only
ksh:  x=unset status=1
mksh: read-only: x
mksh: x=unset status=2
zsh:1: read-only variable: x
zsh:  x=      status=1
dash: 1: readonly: x: is read only
dash: x=unset status=2
bb: readonly: line 1: x: is read only
bb:   x=unset status=2
posh: x: is read only
posh: x=      status=1

Interestingly none of my shells trigger this when the same thing is forced by
`command readonly' even if the variable is ultimately modified or unset.

 $ ( for sh in bash sh ksh mksh zsh dash bb posh; do printf '%-5s x=%5s status=%s\n' "${sh}:" "$("$sh" -c "\${ZSH_VERSION+false}||emulate sh; trap 'echo \${x+unset}' EXIT; $(</dev/fd/3)")" "$?"; done ) 3<<\EOF
x=1 command readonly x=2
EOF

bash: x=unset status=0
sh:   x=unset status=0
ksh:  x=unset status=0
mksh: x=      status=0
zsh:  x=      status=0
dash: x=      status=0
bb:   x=unset status=0
posh: x=      status=0

-- 
Dan Douglas

Back to gnu.bash.bug | Previous | Next | Find similar


Thread

Re: `foo=1 declare -r foo' prints an error message but its exit status is zero Dan Douglas <d@ndougl.as> - 2015-09-11 02:04 -0500

csiph-web