bug-bash
[Top][All Lists]
Advanced

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

Re: declare in a function makes a variable unable to be found with decla


From: SN
Subject: Re: declare in a function makes a variable unable to be found with declare -p in some cases
Date: Wed, 18 Feb 2015 20:30:31 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0

> Also, remember to state the version you're using.
I pasted version in the format printed by bashbug. In case it's not enough:

$ LANG=C bash --version
GNU bash, version 4.3.33(1)-release (x86_64-pc-linux-gnu)
...

> This particular
> feature seems to behave differently in the -devel branch:
>
>
> | $ cat declare.bash
> | #!/bin/bash
> | 
> | for bash in /tmp/bash-* /bin/bash; do
> |   "$bash" --version
> |   printf '%s\n' ---
> |   "$bash" -c 'declare -a a; declare -p a'
> |   "$bash" -c 'declare -a a=(); declare -p a'
> |   "$bash" -c 'declare -a a="()"; declare -p a'
> |   "$bash" -c 'f(){ declare -a a; declare -p a; }; f'
> |   "$bash" -c 'f(){ declare -a a=(); declare -p a; }; f'
> |   "$bash" -c 'f(){ declare -a a="()"; declare -p a; }; f'
> |   printf '%s\n' ---
> | done
> | 
> | $ ./declare.bash
> | GNU bash, version 4.4.0(1)-devel (x86_64-unknown-linux-gnu)
> | Copyright (C) 2014 Free Software Foundation, Inc.
> | License GPLv3+: GNU GPL version 3 or later 
> <http://gnu.org/licenses/gpl.html>
> | 
> | This is free software; you are free to change and redistribute it.
> | There is NO WARRANTY, to the extent permitted by law.
> | ---
> | declare -a a
> | declare -a a=()
> | declare -a a=()
> | declare -a a
> | declare -a a=()
> | declare -a a=([0]="()")
There is an inconsistency in how the declare a="()" is interpreted here,
as you notice below.

However, the main issue in 4.3.33 from the report is that declare -p
does not produce "reusable" output.
This simple test case will show what I mean:

$ f() { declare -a a="()"; eval "declare -p a"; }; f
bash: declare: a: not found

compare with this:

$ f() { declare -a a=(); eval "declare -p a"; }; f
declare -a a='()'

or

$ f() { local a="foo"; eval "declare -p a"; }; f
declare -- a="foo"

(See also:
https://lists.gnu.org/archive/html/bug-bash/2011-12/msg00067.html.)

Thanks for checking it on various versions! In 4.4.0(1)-devel this test
passes for a few variations I tried. For example:

$ f() { declare -a a="()"; eval "declare -p a"; printf "[%s]\n"
"${a[@]}"; }; f
declare -a a=([0]="()")
[()]

and

$ f() { declare -a a=(); eval "declare -p a"; printf "[%s]\n" "${a[@]}";
}; f
declare -a a=()
[]

so it's good.

> You're reporting the behavior of the master branch, but it seems to
> be already fixed in devel. I'm not sure about the inconsistency
> between:
> |   "$bash" -c 'declare -a a="()"; declare -p a'
> and
> |   "$bash" -c 'f(){ declare -a a="()"; declare -p a; }; f'
>
> IMO these two should have the same behaviour.
I guess so.
> Chet will be able to
> clarify this.




reply via email to

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