Re: readonly changes set -e behavior

2017-02-21 Thread Eduardo Bustamante
On Tue, Feb 21, 2017 at 11:02 AM, Dennis Kuhn  wrote:
[...]
>
> When the variable s is set to readonly the script does not exit and echoes 
> "abc":
>
> #!/bin/bash
> set -e
>
> readonly s=$(false)
> echo "abc"
[...]

This is a commonly reported issue. The moment you add the readonly
builtin, you're no longer seeing the exit code from the command
substitution, but the exit code from readonly. See:

https://lists.gnu.org/archive/html/bug-bash/2012-10/msg00075.html
Command substitution and errexit
http://lists.gnu.org/archive/html/bug-bash/2014-08/msg00036.html
'declare' does not honor '-e' in command substituted assignments - a
bug
http://lists.gnu.org/archive/html/bug-bash/2015-09/msg00109.html local
keyword hides return code of command substitution

One easy way to workaround this is to do:

s=$(...); readonly s



readonly changes set -e behavior

2017-02-21 Thread Dennis Kuhn
Hello,

Configuration Information [Automatically generated, do not change]:
Machine: Mac
OS: Darwin
Compiler: gcc
Compilation CFLAGS: Xcode
uname output: Darwin dkuhn 15.6.0 Darwin Kernel Version 15.6.0: Mon Jan  9 
23:07:29 PST 2017; root:xnu-3248.60.11.2.1~1/RELEASE_X86_64 x86_64
Machine Type: x86_64-Apple-Darwin

Bash Version: 3.2
Patch Level: 48
Release Status: relase

Description:
Set a variable readonly changes the behavior of "set -e". Here is a small 
example:

#!/bin/bash
set -e

s=$(false)
echo "abc"

This script exists with exit code 1 as expected after the subshell line.

When the variable s is set to readonly the script does not exit and echoes 
"abc":

#!/bin/bash
set -e

readonly s=$(false)
echo "abc"

This behavior is unexpected. I could reproduce the bug
on ubuntu 16.04 with bash version 4.3-14ubuntu1.1


Regards,
Dennis Kuhn


signature.asc
Description: Message signed with OpenPGP using GPGMail