Live data from Hacker News

Today I learned that bash has hashmaps (2024)

xeiaso.net

51–60 of 137 posts

Re: Today I learned that bash has hashmaps (2024)

#51
post #42

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...

doesn't work in zsh?

Re: Today I learned that bash has hashmaps (2024)

#52
No one else seems to have mentioned this, but POSIX sh does not include this feature.

Until 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)

#55
post #13

It’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)

#57
post #13

It’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.

They suggest that based on their experience. Error handling in bash is awful so I advice against using it for anything complex.

Re: Today I learned that bash has hashmaps (2024)

#58

Not only do they exist, but they have some fantastic foot guns! https://mywiki.wooledge.org/BashPitfalls#A.5B.5B_-v_hash.5B....

"fantastic foot guns"

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)

#59

Nice 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.

I enjoy it quite a lot :shrug:

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)

#60
post #57

Earlier 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.

Anything complex should be written in a competent language like Java. Script languages (like Bash and Python) are for short (a few lines long) scripts. Using the tool outside the scope of what it was designed for is not a good idea.
Post reply on HN