Skip to content

Instantly share code, notes, and snippets.

@mattolenik
Last active September 16, 2018 01:04
Show Gist options
  • Save mattolenik/5a51ab685bded0cf542b7524a690252d to your computer and use it in GitHub Desktop.
Save mattolenik/5a51ab685bded0cf542b7524a690252d to your computer and use it in GitHub Desktop.
Looks for "named arguments" in an array, which take the form key=value
# Searches for a named argument in the form of "key=value".
# Returns the value associated with the key.
#
get_named_arg() {
local key="$1"
local default="${2:-}"
shift 2
for pair in "$@"; do
if [[ $pair =~ ^([^=]+)=([^=]*)$ ]] && [[ ${BASH_REMATCH[1]} == "$key" ]]; then
printf %s "${BASH_REMATCH[2]}"
fi
done
printf %s "$default"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment