danbarrese
9/1/2016 - 4:29 AM

Iterate over array of arrays in BASH

Iterate over array of arrays in BASH

# For each variable starting with "prefix", execute the "fun" function. #>>
# ARGS:
#   prefix: The prefix of all the variables to iterate over.
#   fun: The name of the function to call for each variable.
# DATE: August 31, 2016
# #<<
function foreach {
    local prefix=$1
    local fn=$2
    shift 2
    local ar=
    for ar in $(compgen -A variable |egrep "^${prefix}.+"); do
        ar="${ar}[@]" # prefix0[@]
        ar=(${!ar}) # (a b)
        eval "${fn} ${ar[@]}"
    done
}