On 6.6. 15:53, Greg Wooledge wrote:
wooledg:~$ echo $(( a[$i] ))
Tue 04 Jun 2019 09:23:28 AM EDT
0
wooledg:~$ echo $(( 'a[$i]' ))
bash: 'a[$(date >&2)]' : syntax error: operand expected (error token is "'a[$(date
>&2)]' ")
I definitely got different results when I added single quotes.
Well, yes... The point I was trying to make before (and which Chet
seemed to confirm) is that the quotes break the $((..)) expression
completely, even if 'i' there is just a number, and not any fancier than
that.
$ a=(123 456 789)
$ i=2
$ echo $(( a[$i] ))
789
$ echo $(( 'a[$i]' ))
bash5.0.3: 'a[2]' : syntax error: operand expected (error token is
"'a[2]' ")
$ echo $(( 'a[2]' ))
bash5.0.3: 'a[2]' : syntax error: operand expected (error token is
"'a[2]' ")
And with ((..)), the command substitution runs with a slightly different
i, quotes or not and with $i or i:
$ i='a[$(date >&2)]'
$ (( a[$i]++ ))
Thu Jun 6 16:46:06 EEST 2019
bash5.0.3: a[]: bad array subscript
bash5.0.3: a[]: bad array subscript
$ (( a[i]++ ))
Thu Jun 6 16:46:12 EEST 2019
$ (( 'a[$i]++' ))
Thu Jun 6 16:46:18 EEST 2019
$ (( 'a[i]++' ))
Thu Jun 6 16:46:31 EEST 2019
So if we want "valid" values in the index to work, and invalid ones to
not do anything nasty, I can't seem to find a case where quotes would
help with that.
--
Ilkka Virta / itvi...@iki.fi