4

When I use arithmetic expansion in an array index in bash, like this:

declare -a FILES
declare -i INDEX=0

for FILE in ./*
do
    FILES[((INDEX++))]="$FILE"
done

Do I need a dollar sign in front of ((...))?
So does it have to be:

FILES[((INDEX++))]="$FILE"

or rather:

FILES[$((INDEX++))]="$FILE"

?

In my local copy of bash both variants seem to work - it's version 4.3.30.

I would expect only the latter to work, because I think only that one returns the result of the arithmetic expression. But there: Bash - arithmetic in array index I've read that only the first might work with older versions of bash (?). So which one is actually correct? And why is the first one working? I haven't found a specific answer to that yet.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
erbth
  • 43
  • 5
  • In the question you linked, the issue wasn't related to the version of `bash`. The user had a bug in his script (`about the version problem, nevermind... I found out x is empty when the bug is reached`). – Aserre Oct 18 '16 at 09:28
  • @Aserre: of course you are right, I thougt the user would get a syntax error as mentioned in his comment to the accepted answer, but I didn't realize he is using the `let' keyword in his comment. – erbth Oct 19 '16 at 08:07

2 Answers2

3

In arrays, bash considers expressions between []as arithmetic. Thus

i=2 ; f[i++]=10

is perfect. Writing f[((i++))] is also correct but in this case, (()) is not seen as the arithmetic expansion operator, but as nested parentheses.

Note that ((expr)) evaluates expr, then succeeds if it is true, while$((expr)) is expanded as its value. So f[$((i++))] is also correct.

Finally, f[$i++] is not what you want since $i is expanded first. For instance, i=j ; f[$i++] will be expanded as f[j++].

Remark: a strange feature is that bash expands all it can in arithmetic mode without the $ sign:

$ unset i j k f
$ i=j ; j=k ; k=5 ; f[i++]=10
$ declare -p i j k f
declare -- i="6"
declare -- j="k"
declare -- k="5"
declare -a f='([5]="10")'
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Edouard Thiel
  • 5,878
  • 25
  • 33
1

The dollar sign is required in some contexts but not the others:

$ bash --version
GNU bash, version 4.3.42(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

$ echo ((1+2))
bash: syntax error near unexpected token `('

$ echo $((1+2))
3

$ for ((x=0; x<3;++x)); do echo $x; done
0
1
2

$ for $((x=0; x<3;++x)); do echo $x; done
bash: `$((x=0; x<3;++x))': not a valid identifier

After reading bash man page, the dollar sign is not required in compound commands:

Compound commands are the shell programming constructs. Each construct begins with a reserved word or control operator and is terminated by a corresponding reserved word or operator. Any redirections (see Redirections) associated with a compound command apply to all commands within that compound command unless explicitly overridden.

In most cases a list of commands in a compound command’s description may be separated from the rest of the command by one or more newlines, and may be followed by a newline in place of a semicolon.

Bash provides looping constructs, conditional commands, and mechanisms to group commands and execute them as a unit.

for (( expr1 ; expr2 ; expr3 )) is a compound command and hence the dollar sign is not required to enable arithmetic evaluation.

Whereas echo $((expr)) is not a compound command because it does not start with a reserved bash keyword, so it requires a dollar sign to enable arithmetic evaluation.

Community
  • 1
  • 1
Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
  • That's the point which I don't understand: Why is the dollar sign required with echo, but not in an array index. Why do both variants work in an array index? But I think I can understand the `for' case: I would say that is some kind of special syntax to the for keyword. No `pure' arithmetic expansion, but a special `for' loop constructed by three arithmetic expressions. – erbth Oct 19 '16 at 08:15
  • I'm sorry, haven't understood it yet. Why is the dollar sign not needed in compound commands? Why could the dollar sign be required in the for(()) command if it was required in compound commands? Is there a version of the C-like for command (with 3 parts) that does not use arithmetic evaluation by default? What about array indices? Are they compound commands (maybe because they are programming constucts)? – erbth Oct 21 '16 at 16:24