Today I learned that bash has hashmaps (2024)

xeiaso.net

43 points

stefankuehnel

3 days ago


13 comments

agnishom 2 hours ago

> Q: How do I declare a Hashmap?

> A: You use the command `declare -A HASHMAP_NAME`

This is why I think Bash is a horrible language

  • jolmg an hour ago

    Because of the -A instead of an -H? -A is for "Associative array".

  • arter4 an hour ago

    what do you mean?

oniony 3 days ago

A couple of weeks ago I learnt that Bash on Mac does not have associative arrays. We worked around the issue by changing the script to run under Zsh, but beware.

  • ptdorf 2 days ago

    Sounds like a bash 3 issue.

    $ bash --version

    GNU bash, version 5.2.32(1)-release (aarch64-apple-darwin23.4.0)

    $ declare -A aaa; aaa[a]=a; aaa[b]=bb; for i in ${!aaa[@]}; do echo "$i --> ${aaa[$i]}"; done

    b --> bb

    a --> a

    • forgotpwd16 an hour ago

      To elaborate on this, macOS default bash is still stuck (assuming due to licensing) in v3.2.x (released in 2007). Bash got associative arrays in v4 (released in 2009).

  • x3n0ph3n3 2 hours ago

    You should be getting bash from homebrew anyways.

    • pletnes 4 minutes ago

      Just use the included zsh.