help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] declare -i


From: lxnf98mm
Subject: Re: [Help-bash] declare -i
Date: Wed, 9 May 2012 14:49:15 -0500 (CDT)
User-agent: Alpine 2.00 (LRH 1167 2008-08-23)

On Wed, 9 May 2012, Chet Ramey 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.

Chet



So for the totally ignorant are y'all trying to say that 'declare -i' is evil 
and should never be used

Richard



reply via email to

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