2

I could not access elements from the integer array in bash and add the two.

This is my code:

popA[$n]=${popA[$n]} - ${popA[($n)-1]};

1 Answer 1

4

You are accessing the elements correctly, but you need an arithmetic expression to actually perform math:

popA[$n]=$(( ${popA[$n]} - ${popA[$n - 1]} ))

In case you are wondering why you don't need $((...)) for the n-1 index, the array subscript is automatically evaluated as an arithmetic expression for an indexed array. (The parentheses around $n are unnecessary, although harmless.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.