0

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.

2 Answers 2

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.

1
  • Thanks Choroba, I'll stick to the syntax shown above to avoid any weird bash behaviour, Feb 25, 2014 at 12:50
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."

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .