Variable casts from string to array but silently fails in reverse

2014-01-31 Thread Jonathan Doull
Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64'
-DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-pc-linux-gnu'
-DCONF_VENDOR='pc' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash' -DSHELL
-DHAVE_CONFIG_H   -I.  -I../bash -I../bash/include -I../bash/lib
 -D_FORTIFY_SOURCE=2 -g -O2 -fstack-protector --param=ssp-buffer-size=4
-Wformat -Wformat-security -Werror=format-security -Wall
uname output: Linux moriarty 3.2.0-58-generic #88-Ubuntu SMP Tue Dec 3
17:37:58 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
Machine Type: x86_64-pc-linux-gnu

Bash Version: 4.2
Patch Level: 25
Release Status: release

Description:

Variables can be automatically cast from string to array.
However when cast from array to string, the cast silently fails.

Repeat-By:

Demonstraction script:

#-SCRIPT START   ---
#!/bin/bash
function demonstrate_fail() {

local something

something=""  # setting something to a string
declare -p something

something=(hello world now)   # setting something to an array
declare -p something

something=""  # setting something to a string
declare -p something  # UNEXPECTED BEHAVIOUR : something is
still an array

something=1   # setting something to an integer
declare -p something  # UNEXPECTED BEHAVIOUR : first array
element is updated

}

demonstrate_fail
#-SCRIPT END   ---

Fix:
 Using unset seems only workaround.


Re: Variable casts from string to array but silently fails in reverse

2014-01-31 Thread Jonathan Doull
Thanks - understandable although was confusing at the time and made for a
nasty subtle scripting bug.

Bash is not for the faint hearted.


On 31 January 2014 19:45, Chet Ramey  wrote:

> On 1/31/14 10:39 AM, Jonathan Doull wrote:
>
> > Bash Version: 4.2
> > Patch Level: 25
> > Release Status: release
> >
> > Description:
> >
> > Variables can be automatically cast from string to array.
> > However when cast from array to string, the cast silently fails.
>
> Not quite.  The bash man page says:
>
> "Referencing an array variable without a subscript is equivalent to
> referencing the array with a subscript of 0."
>
> This works for both obtaining and assigning values.  So, you are partially
> correct: assigning to a variable using array assignment syntax converts
> it to an array.  However, once a variable has been converted to an
> array, references without subscripts are equivalent to ${array[0]} or
> array[0]=value.
>
> Chet
> --
> ``The lyf so short, the craft so long to lerne.'' - Chaucer
>  ``Ars longa, vita brevis'' - Hippocrates
> Chet Ramey, ITS, CWRUc...@case.edu
> http://cnswww.cns.cwru.edu/~chet/
>