Created
February 16, 2015 01:00
-
-
Save cig0/86d143b7312919167539 to your computer and use it in GitHub Desktop.
typeset - arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Sum a set of arrays and assign the result indirectly, also printing each intermediary result (without portability workarounds) | |
# sum name arrname [ arrname ... ] | |
function sum { | |
typeset -n _result=$1 _arr | |
typeset IFS=+ | |
_result=0 | |
for _arr in "${@:2}"; do # Demonstrate the special property of "for" on a nameref. | |
(( _result += ${_arr[*]} )) | |
printf '%s = %d\n' "${!_result}" "$_result" # Demonstrate the special property of ${!ref} on a nameref. | |
done | |
} | |
a=(1 2 3) b=(6 5 4) c=(2 4 6) | |
sum total a b c | |
printf 'Final value of "total" is: %d\n' "$total" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment