bug-bash
[Top][All Lists]
Advanced

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

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


From: Dan Douglas
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
User-agent: KMail/5.0.43 pre (Linux/4.2.0; KDE/5.14.0; x86_64; ; )

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

Attachment: signature.asc
Description: This is a digitally signed message part.


reply via email to

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