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 11:24:36 -0600

On Tue, May 8, 2012 at 10:03 PM, Dan Douglas <address@hidden> wrote:
Using -i can be an easy source of bugs and security problems, and adds a lot
of
quirks especially with arrays.

-- Dan Douglas

Is this one of the quirks you were talking about:

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'.

Is there a list of these anomalies somewhere?

--
Bill Gradwohl


reply via email to

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