bug-bash
[Top][All Lists]
Advanced

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

Re: Code Execution in Mathematical Context


From: Ilkka Virta
Subject: Re: Code Execution in Mathematical Context
Date: Thu, 6 Jun 2019 16:59:59 +0300
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:60.0) Gecko/20100101 Thunderbird/60.7.0

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 / address@hidden



reply via email to

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