File name: test11. SH
#!/bin/bash# Adding valuesinchAn arrayfunctionAddArray {Localsum=0Local NewArray NewArray=("[email protected]") forValueinch${newarray[*]} Do sum=$[ $sum+$value] Done Echo$sum}myarray=(1 2 3 4 5)Echo "The original array is: ${myarray[*]}"Argl="${myarray[*]}"result="AddArray $argl"Echo "The result is $result"
The result of the run-after output is:
1 2 3 4 5 123)45
And don't get what you want and that's not clear
Error found: result="addarray $argl" in the addarray $argl the left and right side of the "symbol, is the upper left-hand corner of the key, the book to see the single quotation marks, a thought how may be single quotation marks, single quotation marks will be the characters inside as a string, but the use of double quotation marks is not the result, and later see other books on the script, found that the reference function, the outside symbol is ', changed the result can be long
Output Result:
1 2 3 4 5 the
Use functions to accumulate the sum of the values in the array