Last active
October 28, 2022 18:34
-
-
Save samuelneff/184762f54fd5016fdb4308abea051e92 to your computer and use it in GitHub Desktop.
Bash hash functions
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
# Simplified hash structure creation/set/get | |
# Based on https://stackoverflow.com/a/2225712/118703, but modified. | |
hashKey() { | |
# replace non-alphanumeric characters with underscore to make keys valid BASH identifiers | |
echo "$1_$2" | sed -E "s/[^a-zA-Z0-9]+/_/g" | sed -E "s/^[^a-zA-Z0-9]+|[^a-zA-Z0-9]+\$//g" | |
} | |
hashPut() { | |
local KEY=`hashKey $1 $2` | |
eval "$KEY"="$3" | |
} | |
hashGet() { | |
local KEY=`hashKey $1 $2` | |
echo "${!KEY}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment