[Top][All Lists]

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

Re: local assigned from command substitution disobeys set -eu

From: Chet Ramey
Subject: Re: local assigned from command substitution disobeys set -eu
Date: Tue, 30 Dec 2014 09:46:49 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:31.0) Gecko/20100101 Thunderbird/31.3.0

On 12/30/14 7:03 AM, Ville Oikarinen wrote:
> Hello.
> I often use "set -eu" to make my scripts more robust by exiting when
> there are errors.
> But one of my scripts just kept on running after failures. I found out
> that the keyword "local" does not obey it. Here is a script to
> reproduce the problem:

This is not a bug.

> #!/bin/bash
> set -eu
> failure() {
>     echo "going to fail"
>     false
> }
> cmd-subst-failure-with-local() {
>     echo "calling failure without failing because of local"
>     local FAILUREOUT=$(failure)
>     echo "Bug: set -eu wasn't effective: failure output was '$FAILUREOUT'"
> }
> cmd-subst-failure-without-local() {
>     echo "calling failure so it fails (without local)"
>     FAILUREOUT=$(failure)
>     # here set -eu works so we will fail instead of getting here
>     echo "Internal error, set -eu didn't work even without local"
> }
> cmd-subst-failure-with-local

The `local' builtin succeeds because the assignment is performed succesfully.

> cmd-subst-failure-without-local

In the absence of a command name, the assignment statement's return
status is dependent on whether or not there is a command substitution
in the assignment statement, and that command substitution's exit

``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    address@hidden    http://cnswww.cns.cwru.edu/~chet/

reply via email to

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