[Top][All Lists]

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

Re: Bash 4.3 handles array variables in sourced scripts differently than

From: Ewan Mellor
Subject: Re: Bash 4.3 handles array variables in sourced scripts differently than 4.2
Date: Mon, 10 Mar 2014 18:57:40 +0000
User-agent: Microsoft-MacOutlook/

On 3/10/14, 6:50 AM, "Chet Ramey" <address@hidden> wrote:

>On 3/9/14 8:45 PM, Ewan Mellor wrote:
>> Hi,
>> Please cc me ‹ I¹m not subscribed to this list.
>> I have a script that works correctly in Bash 4.2.25(1)-release from
>> 12.04 (package version 4.2-2ubuntu2) but that fails in Bash
>> 4.3.0(1)-release from Ubuntu 14.04 prerelease (package version
>> 4.3-2ubuntu1).
>> The root of the problem is that I am using declare ­a inside a script
>> is sourced.  In 4.2 this variable is still available in the outer scope.
>> In 4.3 it is not, and I get an error when I try to use it.
>This doesn't really have anything to do with source, and very little to do
>with scope.  A variable is not set until it has been assigned a value.  A
>statement like `declare -a on_exit_hooks' creates a `placeholder', but the
>variable remains unset until it's been assigned a value.  Setting
>attributes for a variable can change how it behaves when a value is
>assigned, but does not assign a value itself.  This is true for all
>variables, not just arrays.
>When you attempt to dereference on_exit_hooks in add_on_exit using
>${#on_exit_hooks[*]}, you get an unbound variable error because you've
>enabled `set -u'.
>This was a bug in bash-4.2; bash-4.3 tightened up the set/unset behavior.
>It looks like I put in some changes in this behavior in bash-4.3-beta:
>f.  Fixed several cases where `invisible' variables (variables with
>    but no values, which are technically unset) were treated incorrectly.
>I'm sure there are still some inconsistencies there.
>You can add a line like
>after the declare command and have something that works in both bash-4.2
>and bash-4.3.

Thanks for the prompt reply, Chet.

I got this originally from
ry-files, so even if it was a bug, it’s something that people have been
relying on since 2009.  That’s worth a mention in the FAQ I think.

I’m happy using the change you suggested though -- that gets me past my




reply via email to

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