[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] declare -i
From: |
Dan Douglas |
Subject: |
Re: [Help-bash] declare -i |
Date: |
Wed, 09 May 2012 16:20:32 -0500 |
User-agent: |
KMail/4.8.3 (Linux/3.3.4-pf+; KDE/4.8.3; x86_64; ; ) |
On Wednesday, May 09, 2012 11:24:36 AM Bill Gradwohl wrote:
>
> > 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')
Yes that's one of them. -i gets set after the argument(s) are processed. This
is yet another thing affected by everything disucssed in your "indirection"
thread.
$ declare -ai a=(1 1+1 3); declare -p a
declare -ai a='([0]="1" [1]="1+1" [2]="3")'
$ declare -ai 'a=(1 1+1 3)'; declare -p a
declare -ai a='([0]="1" [1]="2" [2]="3")'
And it's also required to modify a global array:
$ a=(1 2 3); f() { local a=(a b c); declare -ga 'a+=(4)'; declare -p a; };
f; declare -p a
declare -a a='([0]="a" [1]="b" [2]="c")'
declare -a a='([0]="1" [1]="2" [2]="3" [3]="4")'
But this is probably a bug:
$ a=(1 2 3); f() { local a=(a b c); declare -ga a+=(4); declare -p a; }; f;
declare -p a
-bash: declare: `a+': not a valid identifier
declare -a a='([0]="a" [1]="b" [2]="c" [3]="4")'
declare -a a='([0]="1" [1]="2" [2]="3")'
Most of the anomolies aren't necessarily "bugs", just undocumented or conflict
with documented things. One is that += can unset the zeroth element in Bash if
the index isn't given explicitly. += doesn't unset elements in any other case.
$ declare -ia a=(2 2 3); a+=1; declare -p a
declare -ai a='([0]="1" [1]="2" [2]="3")'
Ksh doesn't have this problem:
$ typeset -ia a=(2 2 3); a+=1; typeset -p a
typeset -a -i a=(3 2 3)
Another the manual doesn't mention is that negative indexes can't be used on
assignment, even if the array already exists (ksh can, but zsh and mksh
completely disagree here too):
$ declare -ia a=(1 2 3); read -r 'a[-1]' <<<'a[-1]+1'; declare -p a
-bash: a[-1]: bad array subscript
declare -ai a='([0]="1" [1]="2" [2]="3")'
$ typeset -ia a=(1 2 3); read -r 'a[-1]' <<<'a[-1]+1'; typeset -p a
typeset -a -i a=(1 2 4)
Bash is the only shell that evaluates expansions in an array index indirectly,
so getting strings into your integer array can be dangerous. Here's a forkbomb
that would probably stump most people (careful):
declare -i _=('_[`_=_&`]')
--
Dan Douglas
signature.asc
Description: This is a digitally signed message part.
- Re: [Help-bash] declare -i, (continued)
- Re: [Help-bash] declare -i, Bill Gradwohl, 2012/05/10
- Re: [Help-bash] declare -i, Chet Ramey, 2012/05/10
- Re: [Help-bash] declare -i, Greg Wooledge, 2012/05/10
- Re: [Help-bash] declare -i, Chet Ramey, 2012/05/10
- Re: [Help-bash] declare -i, Dan Douglas, 2012/05/10
- Re: [Help-bash] declare -i, Bill Gradwohl, 2012/05/10
- Re: [Help-bash] declare -i, Clark WANG, 2012/05/09
- Re: [Help-bash] declare -i, Chet Ramey, 2012/05/09
- Re: [Help-bash] declare -i, lxnf98mm, 2012/05/09
- Re: [Help-bash] declare -i, Greg Wooledge, 2012/05/09
- Re: [Help-bash] declare -i,
Dan Douglas <=
- Re: [Help-bash] declare -i, Chet Ramey, 2012/05/10