Re: Bash 4.1 doesn't behave as I think it should: arrays and the environment
On Fri, Aug 17, 2012 at 10:19 AM, John Summerfield wrote: (...) > The man page for bash contains a para entitled ENVIRONMENT which doesn't > mention arrays, leaving the reader to assume they are not different from > other shell variables. the BUGS section contains: Array variables may not (yet) be exported.
bash does filename expansion when assigning to array member in compound form
This is a question about bash's behaviour concerning assignment to array members. Bash Version 4.2.24(1)-release (x86_64-pc-linux-gnu) The bash manual (version 4.1, chapter 3.4) says about assignment to shell variables: "Filename expansion is not performed." simple test to proof this on the command line: $ varx=*.*; echo "$varx" => *.* This is true also for assignment to array members. $ vary[0]=*.*; echo "${vary[0]}" => *.* But when compound form of asignment is used, filename expansion _IS_ performed. $ varz=(*.*); echo "${varz[0]}" => file1.abc file2.xyz If this is a feature, I can't find any documentation. Is this a bug? Thank you for your help Gundi
Re: bash does filename expansion when assigning to array member in compound form
This is a feature that all shells with this style of compound assignment have in common. If no explicit subscripts are given, the text between the parentheses is processed exactly as though it were arguments to a command including brace expansion, word-splitting, and pathname expansion (and consequently, quoting is just as important). This is an important feature because it allows storing the results of a glob in an array easily. If a subscript is given explicitly, then the right-hand side of the assignment is treated exactly as an ordinary scalar assignment would be, including all analagous behaviors for `+=' and the integer attribute. $ set -x; a=( [1]=* ) + a=([1]=*) -- Dan Douglas
Re: bash does filename expansion when assigning to array member in compound form
2012-08-18 10:26:22 -0500, Dan Douglas: > This is a feature that all shells with this style of compound assignment have > in common. If no explicit subscripts are given, the text between the > parentheses is processed exactly as though it were arguments to a command > including brace expansion, word-splitting, and pathname expansion (and > consequently, quoting is just as important). This is an important feature > because it allows storing the results of a glob in an array easily. > > If a subscript is given explicitly, then the right-hand side of the > assignment > is treated exactly as an ordinary scalar assignment would be, including all > analagous behaviors for `+=' and the integer attribute. > > $ set -x; a=( [1]=* ) > + a=([1]=*) [...] Nope: ~/1$ touch '[1]=x' ~/1$ bash -c 'a=( [1]=* ); echo "${a[@]}"' [1]=x ~/1$ bash -c 'a=( [1]=asd ); echo "${a[@]}"' asd That's a bug though. Just do a=("*") or a=('*') or a=(\*) -- Stephane
Re: bash does filename expansion when assigning to array member in compound form
On Saturday, August 18, 2012 07:55:17 PM Stephane Chazelas wrote: > 2012-08-18 10:26:22 -0500, Dan Douglas: > > This is a feature that all shells with this style of compound assignment > > have > > in common. If no explicit subscripts are given, the text between the > > parentheses is processed exactly as though it were arguments to a command > > including brace expansion, word-splitting, and pathname expansion (and > > consequently, quoting is just as important). This is an important feature > > because it allows storing the results of a glob in an array easily. > > > > If a subscript is given explicitly, then the right-hand side of the > > assignment > > is treated exactly as an ordinary scalar assignment would be, including all > > analagous behaviors for `+=' and the integer attribute. > > > > $ set -x; a=( [1]=* ) > > + a=([1]=*) > [...] > > Nope: > > ~/1$ touch '[1]=x' > ~/1$ bash -c 'a=( [1]=* ); echo "${a[@]}"' > [1]=x > ~/1$ bash -c 'a=( [1]=asd ); echo "${a[@]}"' > asd > > That's a bug though. > > Just do > > a=("*") or a=('*') or a=(\*) > > Eh yeah. At least the left side gets implicit quoting, and it correctly disables brace expansion. In mksh compound assignment is just sugar for set -A, so Bash isn't unique in this. $ touch 1=a; mksh -c 'a=([123]=*); print -r "${a[@]}"' 1=a -- Dan Douglas
Re: bash does filename expansion when assigning to array member in compound form
Bleh I'm wrong, brace expansion remains too. I should know this... it's hard to remember all the quirks even when I write them down.