for context, this came up when trying to do $((foo[bar[i%${#bar[@]}]])) which has no immediate equivalent without $
On Mon, Jun 30, 2025, 19:45 Robert Elz <k...@munnari.oz.au> wrote: > Date: Mon, 30 Jun 2025 10:44:24 -0400 > From: Greg Wooledge <g...@wooledge.org> > Message-ID: <20250630144424.gj32...@wooledge.org> > > | Unfortunately, that's not true. You need $ if you want to use any > | special parameters in an arithmetic context, such as $# or $1. > > There are a zillion other reasons why it can be needed as well, > One is to have a variable operator in the expression (say op=+ or op=- > and then use ${op} as a binary operator.) > > Further I often embed command substitutions within arithmetic, that's > useful as well (and unless the results would be different using the old > form cmdsub (`...`) which it doesn't seem to be) this really should be > fixed (I personally don't care much about the (()) command, but the > same issue arises in $(( )) -- though that's not surprising, the two > should be the same for everything except the final result use). > > And even more, it should be possible to have the entire expression > be the result of a variable expansion > ((${exp})) > and (aside from no recursive expansions) that should evaluate exactly > the same as > eval "((${exp}))" > would. > > > What's more, I don't see what the issue is, inside (( )) or $(( )) > is what should be treated as a double quoted string (arith context > for evaluation, but still a double quoted string - and the first thing > the shell should be doing with any double quoted string, in any context, > is to do any var/cmdsub/arith expansions that occur within it. Then > it gets used for whatever purpose it has. > > Do that in the given example, and > > ((i=$x,a[b[i]])) > turns into > ((i=0,a[b[i]])) > > which does work. Of course, I know nothing about how any of this > is implemented inside bash, but this just seems simple to me, and > if as: > > > chet.ra...@case.edu said: > | This was one of the results of this 2021 discussion: > | https://lists.gnu.org/archive/html/bug-bash/2021-03/msg00056.html > | You can get the result you want by not using the $. > > then something went very badly wrong with what happened there, and > simply saying "don't use the $" is not nearly good enough. > > kre > > > >