help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] declare -i


From: Bill Gradwohl
Subject: Re: [Help-bash] declare -i
Date: Wed, 9 May 2012 13:39:43 -0600

On Wed, May 9, 2012 at 12:57 PM, Chet Ramey <address@hidden> wrote:
On 5/9/12 1:24 PM, Bill Gradwohl wrote:

> declare -i -a intArray=(' 5' ' 2 ' 1 '  -19  ' 'xyz')
> declare -p intArray
> intArray[0]=' 5'
> declare -p intArray
>
> unset intArray
> declare -i -a intArray
> intArray=(' 5' ' 2 ' 1 '  -19  ' 'xyz')
> declare -i -a
>
>
> Outputs:
> address@hidden ycc# ../tst
> declare -ai intArray='([0]=" 5" [1]=" 2 " [2]="1" [3]="  -19  " [4]="xyz")'
> declare -ai intArray='([0]="5" [1]=" 2 " [2]="1" [3]="  -19  " [4]="xyz")'
> declare -ai intArray='([0]="5" [1]="2" [2]="1" [3]="-19" [4]="0")'
>
> I added the bogus xyz just to see what happens. It was after I noticed that
> when provided at the time of declaration, the array holds elements it would
> not otherwise hold. When the same ( ... ) _expression_ is used to initialize
> the array after the array already exist, you get a completely different
> outcome.
>
> Trying to change the existing [0] which contains ' 5' to exactly the same
> thing instead produces just '5'.

This is unsurprising, since you told the shell you wanted arithmetic
expansion performed at assignment.  The potential bug, as you noted,
is that the first line of output is not the same as the third.


The first declare does not get arithmetic expansion on the 'xyz' element. It get 'xyz'. I thought it would get a 0.

Depending on how you look at it, it also doesn't get arithmetic expansion on the ' 5', as it keeps the leading space. I thought all the extraneous white space would be gone. It's not.

--
Bill Gradwohl


reply via email to

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