[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: |
Chet Ramey |
Subject: |
Re: Bash 4.3 handles array variables in sourced scripts differently than 4.2 |
Date: |
Mon, 10 Mar 2014 09:50:13 -0400 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.1.1 |
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 Ubuntu
> 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 that
> 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 attributes
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
on_exit_hooks=()
after the declare command and have something that works in both bash-4.2
and bash-4.3.
Chet
--
``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/