One thing the article doesn't mention is how you can use indirect expansion to get a list of all keys. For example: `${!myvar[@]}` would list all of the keys. I've written about associative arrays in Bash a bit here: https://nickjanetakis.com/blog/associative-arrays-in-bash-ak...
Today I learned that bash has hashmaps (2024)
51–60 of 137 posts
Re: Today I learned that bash has hashmaps (2024)
#52Until it does, and major POSIX shs have shipped with it for a decade, then the feature will actually exist in a way that the average shell coder cares about. You're better off just shipping Rust/Go/etc binaries and leave sh for your simple glue tasks.
Even I've eventually switched to this, and I've written 10k+ long Bash scripts that followed best practices and passed shellcheck's more paranoid optional tests.
Use the right language for the right job.
Re: Today I learned that bash has hashmaps (2024)
#53Re: Today I learned that bash has hashmaps (2024)
#54Re: Today I learned that bash has hashmaps (2024)
#55It’s amazing how much of a superpower merely reading the manual is nowadays. https://www.gnu.org/software/bash/manual/bash.html#Arrays >
Re: Today I learned that bash has hashmaps (2024)
#56Re: Today I learned that bash has hashmaps (2024)
#57It’s amazing how much of a superpower merely reading the manual is nowadays. https://www.gnu.org/software/bash/manual/bash.html#Arrays >
Bash is low on the list of things to learn, especially as many greybeards suggest keeping on POSIX compatibility and/or using a "proper" language (Python, older: Perl) for anything longer than a few lines.
Re: Today I learned that bash has hashmaps (2024)
#58Not only do they exist, but they have some fantastic foot guns! https://mywiki.wooledge.org/BashPitfalls#A.5B.5B_-v_hash.5B....
It wouldn't be a worthy bash feature if after learning about it you wouldn't spend a few days figuring out why the damn thing doesn't work the way it works in any other language.
Re: Today I learned that bash has hashmaps (2024)
#59Nice that these exist—but does anyone else absolutely abhor shell programming? The syntax is impossible to memorize, it’s incredibly easy to make mistakes, and debugging is a pain. I hate it more than C++ and AppleScript.
Bash itself isn't a very big language, so I wouldn't call it "impossible to memorize".
Re: Today I learned that bash has hashmaps (2024)
#60Earlier quoted context omitted.
Bash is low on the list of things to learn, especially as many greybeards suggest keeping on POSIX compatibility and/or using a "proper" language (Python, older: Perl) for anything longer than a few lines.
They suggest that based on their experience. Error handling in bash is awful so I advice against using it for anything complex.