Please reopen. This is not a duplicate, because here I am asking why it doesn't work, not just for workaround.
Bash manual says that
tilde expansion, parameter and variable expansion, arithmetic expansion, and command substitution (done in a left-to-right fashion)
If I understand http://unix.stackexchange.com/a/270324/674 correctly, "left-to-right" means that "brace expansion; tilde expansion, parameter and variable expansion, arithmetic expansion, and command substitution" have the same priority.
So is it possible to use arithmetic expansion inside parameter expansion? (i.e. one level recursion)
If no, why can't arithmetic expansion work inside parameter expansion, given that "tilde expansion, parameter and variable expansion, arithmetic expansion, and command substitution (done in a left-to-right fashion)"?
If yes, how?
For example,
$ set hello world
$ echo $2
world
$ echo ${$((1+1))}
bash: ${$((1+1))}: bad substitution
I hope to
- first expand
$((1+1))
in${$((1+1))}
to2
. and - then
${2}
toworld
.
Thanks.
set 6 7; echo $(($1*$2))
will work as expected. – Philippos Sep 27 '17 at 06:53$(( 1 + ${a:-5} ))
will work but${ $((1 + 1)) }
won't. To solve your requirement you would needset 4 5; b=$((1+1)); echo ${!b}
– Chris Davies Sep 27 '17 at 12:14${undefined:=foo}${undefined}
evaluates tofoofoo
whereas it would evaluate tofoo
if the evaluation was right-to-left. Likewise, left-to-right means that in$(cmd1)$(cmd2)
,cmd1
is executed beforecmd2
. – xhienne Sep 27 '17 at 12:39${@:$((1+1)):1}
would work though ;-) Hint:${@:1+1:1}
would work too. – xhienne Sep 27 '17 at 12:51