diff options
| author | Xe Iaso <me@xeiaso.net> | 2024-01-15 14:18:09 -0500 |
|---|---|---|
| committer | Xe Iaso <me@xeiaso.net> | 2024-01-15 14:18:09 -0500 |
| commit | 7904f8ce518ce36df1b346a7a0c7786404ffb1ae (patch) | |
| tree | d7ddc9ca81e6fc825e15f072a1f093bcb1271afc | |
| parent | f555dd9b6290d28acbb1442419a4e256667b5a98 (diff) | |
| download | xesite-7904f8ce518ce36df1b346a7a0c7786404ffb1ae.tar.xz xesite-7904f8ce518ce36df1b346a7a0c7786404ffb1ae.zip | |
notes: bash has hashmaps, oh god
Signed-off-by: Xe Iaso <me@xeiaso.net>
| -rw-r--r-- | lume/src/notes/2024/bash-hashmap.mdx | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/lume/src/notes/2024/bash-hashmap.mdx b/lume/src/notes/2024/bash-hashmap.mdx new file mode 100644 index 0000000..bafb478 --- /dev/null +++ b/lume/src/notes/2024/bash-hashmap.mdx @@ -0,0 +1,75 @@ +--- +title: Today I learned that bash has hashmaps +date: 2024-01-15 +tags: + - bash + - til + - crimes +--- + +Hashmaps (associative arrays) are a great way to store a bag of key-value data. At work I was writing something that needed me to spawn a bunch of GPU instances, GPU availability is spread out by region and GPU type. I wanted to store a mapping of GPU kind to region name and for some reason I thought it would be a good idea to do it in bash. I was horrified to find out that bash has hashmaps, and decided to write this note to tell you how to use them. + +Here's how you do it: + +First, declare the hashmap with the `declare` builtin: + +```shell +# -A means associative array (hashmap) +declare -A FLY_REGIONS +``` + +Then add some values into it [from the fly.io documentation](https://fly.io/docs/gpus/gpu-quickstart/): + +```shell +FLY_REGIONS["a100-40gb"]="ord" +FLY_REGIONS["a100-80gb"]="mia" +FLY_REGIONS["l40s"]="ord" +``` + +You can also predeclare the array with values in a read-only state: + +```sh +# -A means associative array (hashmap) +# -r means read-only +declare -A -r FLY_REGIONS=( + ["a100-40gb"]="ord" + ["a100-80gb"]="mia" + ["l40s"]="ord" +) +``` + +You can look up values with the same syntax you used to set them: + +```shell +echo "${FLY_REGIONS["a100-40gb"]}" # ord +``` + +You can iterate over the keys with a `for` loop on the `@` value: + +```shell +for size in "${!FLY_REGIONS[@]}"; do + echo "size: $size, region: ${FLY_REGIONS[$size]}" +done +``` + +You can iterate over the values with a `for` loop on the `*` value: + +```shell +for region in "${FLY_REGIONS[@]}"; do + echo "region: $region" +done +``` + +You can delete individual keys with `unset`: + +```shell +unset FLY_REGIONS["a100-40gb"] +``` + +If you need to delete the entire hashmap, you can also do it with `unset`: + +```shell +unset FLY_REGIONS +``` + +Please don't use this for evil. |
