Bash Return Multiple Values from a Function using an Associative Array: Difference between revisions
Jump to navigation
Jump to search
Line 12: | Line 12: | ||
=Function Declaration= | =Function Declaration= | ||
<syntaxhighlight lang='bash'> | <syntaxhighlight lang='bash'> | ||
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}" | |||
} | |||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 03:09, 5 January 2021
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}"
}