Bash Return Multiple Values from a Function using an Associative Array

From NovaOrdis Knowledge Base
Jump to: navigation, search

Internal

Invocation

declare -A CONFIG
load-config CONFIG
echo ${CONFIG["SOMETHING"]}

Function Declaration

function load-config() {
  local map_var_name=$1
  declare -A | grep -q "declare -A ${map_var_name}" || fail "no ${map_var_name} associative array declared"
  local key="SOMETHING"
  local value="BLAH"
  eval "${map_var_name}[\"${key}\"]=${value}"
}