Skip to content

Instantly share code, notes, and snippets.

@samuelneff
Last active October 28, 2022 18:34
Show Gist options
  • Save samuelneff/184762f54fd5016fdb4308abea051e92 to your computer and use it in GitHub Desktop.
Save samuelneff/184762f54fd5016fdb4308abea051e92 to your computer and use it in GitHub Desktop.
Bash hash functions
# 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