I’m trying to increment a value in an array by 1 using the following code, however I’m having some problems with it. Please can someone help me out?
myArray[$position]=((${myArray[$position]}++))
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Try this
The
(( .... ))can perform bash/ksh’s math operations, and the variables referenced inside, don’t need to be passed out as in your example, you’re probably thinking of a similar constructvar=$(( ... MathStuff ...)) OR var=$( ... stringStuff ... )(note the ‘$’ before the opening paren).Also note that inside
(( ... ))you don’t need to use the leading ‘$’ for any math variables like $pct or $counter. If you’re using arguments to the script or a function like $1, $2, … $N, THEN you need to use the $, so the value of $1 is used, instead of just ‘1’. Thanks to @ChrisDown for the reminder!I hope this helps.