Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > gnu.bash.bug > #11486
| 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 | 2015-09-11 02:04 -0500 |
| Message-ID | <mailman.901.1441955106.19560.bug-bash@gnu.org> (permalink) |
| References | <D447E11E-FFFF-4AE2-B737-1885D6C4FE75@qq.com> |
[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
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