In associative arrays, assigning index and value at the same time

0

1

In a bash script I found the following but though it works I cannot understand its logic

REMOTESNAPS+=(["$FS"]=" $SNAP")

The statement above adds an element to the associative array REMOTESNAP by defining which index "$FS" must be used and by adding the second element "$SNAP" to it.

I could not find an explanation for such smart way to build associative arrays nor the point in the manual of bash where it's explained or I can deduce it.

Can somebody expand this topic? It'll be really appreciated.

user2984629

Posted 2014-02-25T12:15:43.053

Reputation: 23

Answers

0

In the "PARAMETERS" section in man bash, see

In the context where an assignment statement is assigning a value to a shell variable or array index, the += operator can be used to append to or add to the variable's previous value. When += is applied to a variable for which the integer attribute has been set, value is evaluated as an arithmetic expression and added to the variable's current value, which is also evaluated. When += is applied to an array variable using compound assignment (see Arrays below), the variable's value is not unset (as it is when using =), and new values are appended to the array beginning at one greater than the array's maximum index (for indexed arrays) or added as additional key-value pairs in an associative array.

choroba

Posted 2014-02-25T12:15:43.053

Reputation: 14 741

Thanks Choroba, I'll stick to the syntax shown above to avoid any weird bash behaviour, – user2984629 – 2014-02-25T12:50:37.140

0

Just a link where I found an example of what explained in the man bash. Search for "Individual mass-setting for associative arrays. The named indexes (here: S1 and S2) are strings."

user2984629

Posted 2014-02-25T12:15:43.053

Reputation: 23