Re: declare in a function makes a variable unable to be found with declare -p in some cases

2015-02-20 Thread SN
> Yes. The issue is that the assignment doesn't make the variable visible,
> when it clearly should.  I've attached a patch that will fix that.

And it does.

> The next version of bash will do things differently, as noted in a
couple of followups to this message. The proposal that prompted the
changes is part of a long thread from December:
http://lists.gnu.org/archive/html/bug-bash/2014-12/msg00115.html

Thanks to you both for the answers. I am glad to see that my original
issue has been fixed.
The thread is indeed long and detailed, so let me just leave a note
about a weird behaviour with 4.4.0 (devel) I have that may or may not be
the intended behaviour.

bash-4.4$ e='(`uname >&2`)'; declare -a a=$e; echo "[$a]"
Linux
[]
bash-4.4$ e='(`uname >&2`)'; declare -a a=$e; echo "[$a]"
[(`uname >&2`)]

As you can see, only in the second case uname is not run. (One can also
type "unset a" to have it execute again.) So there seems to be another
inconsistency.

By the way, I use `cmd` instead of $() because I have this:
$  : $(echo x)
bash: command substitution: line 8: syntax error near unexpected token `)'
bash: command substitution: line 8: `echo x)'



Re: declare in a function makes a variable unable to be found with declare -p in some cases

2015-02-18 Thread SN
> 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 
> 
> | 
> | 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.




Re: declare in a function makes a variable unable to be found with declare -p in some cases

2015-02-17 Thread SN
> Already reported:
>
> * test -v: http://lists.gnu.org/archive/html/bug-bash/2014-11/msg00099.html
> * declare -p arrname: 
> https://lists.gnu.org/archive/html/bug-bash/2012-11/msg00084.html

The second thread is about something completely different as I
understand it.
It is about variables whose values aren't explicitly assigned a value.

Here:

# OK
$ x() { declare -a var=(); declare -p var; }; x
declare -a var='()'
# not OK
$ y() { declare -a var='()'; declare -p var; }; y
bash: declare: var: not found

in both cases a value is assigned, yet the shell behaves differently in the 
second one.




declare in a function makes a variable unable to be found with declare -p in some cases

2015-02-16 Thread SN
Hello all,

I have found a problem with the declare builtin.

Patch Level: 33
Release Status: release

Description:
Apparently, there is a problem with how bash interprets some
variable assignments.
It only happens in a function (probably related to `declare'
making variables local).

Repeat-By:
# OK
$ x() { declare -a var=(); declare -p var; }; x
declare -a var='()'
# not OK
$ y() { declare -a var='()'; declare -p var; }; y
bash: declare: var: not found

Note that the format used in y is what `declare -p' displays.

By the way, empty arrays seem to be reported as "not set".
a=(); test -v a || echo "not set"
This might be related (or not), but consider that the return
status of `test -v'
is different on a bash version unaffected by the bug that I have
tried (4.2.37).