[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Awkward behavior of empty arrays
From: |
Greg Wooledge |
Subject: |
Re: [Help-bash] Awkward behavior of empty arrays |
Date: |
Thu, 31 Aug 2017 10:57:57 -0400 |
User-agent: |
NeoMutt/20170113 (1.7.2) |
On Thu, Aug 31, 2017 at 10:47:43AM -0400, Chet Ramey wrote:
> On 8/31/17 5:51 AM, Andy Chu wrote:
> > # arrays compare equal because they're coerced to strings before comparison
> > $ [[ "address@hidden" == "address@hidden" ]]; echo $?
> > 0
>
> Because the operands in [[ commands don't undergo word splitting.
Oh, man. I didn't read that far down in Andy's message.
It's "wrong" because it doesn't take into account the indices:
wooledg:~$ unset A B; A=([1]=x [12]=y) B=(x y)
wooledg:~$ [[ "address@hidden" = "address@hidden" ]] && echo same!
same!
Are those really the same array? I guess that depends on your
application.
It's also *wrong* (no quotes) because it doesn't take into account
that array elements may contain spaces.
wooledg:~$ unset A B; A=("foo bar") B=(foo bar)
wooledg:~$ [[ "address@hidden" = "address@hidden" ]] && echo same!
same!
No matter what your application is, that's not right.
The only way to compare two arrays for equivalence correctly is to write
a loop.
That said, I've never written a shell script that needed to compare two
arrays for equivalence. I have to wonder what the actual application is.
- [Help-bash] Awkward behavior of empty arrays, Cristian Zoicas, 2017/08/31
- Re: [Help-bash] Awkward behavior of empty arrays, Andy Chu, 2017/08/31
- Re: [Help-bash] Awkward behavior of empty arrays, Greg Wooledge, 2017/08/31
- Re: [Help-bash] Awkward behavior of empty arrays, Chet Ramey, 2017/08/31
- Re: [Help-bash] Awkward behavior of empty arrays, Andy Chu, 2017/08/31
- Re: [Help-bash] Awkward behavior of empty arrays, DJ Mills, 2017/08/31
- Re: [Help-bash] Awkward behavior of empty arrays, Andy Chu, 2017/08/31
- Re: [Help-bash] Awkward behavior of empty arrays, Greg Wooledge, 2017/08/31
- Re: [Help-bash] Awkward behavior of empty arrays, Chet Ramey, 2017/08/31
Re: [Help-bash] Awkward behavior of empty arrays, Chet Ramey, 2017/08/31
Re: [Help-bash] Awkward behavior of empty arrays, Chris F.A. Johnson, 2017/08/31