Live data from Hacker News

Today I learned that bash has hashmaps (2024)

xeiaso.net

41–50 of 137 posts

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

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

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

#43
post #25

Earlier quoted context omitted.

You should probably read the release notes for new major versions. Also, anytime you think “ I wish there was available in this program ”, you should probably double-check if such a feature has appeared since you last learned the program.

That's how I discovered the '&' regex feature in less some years back.

Do you mean PCRE’s “(?&name)” feature? That is not portable; for instance, Python calls it “(?P=name)”. Or do you mean the common feature of “&” in the replacement string meaning “the whole matched string”? While this feature is common in languages which use regexps (including ed, sed, awk, etc.), that’s not actually a regexp feature, since the & character has that effect in the replacement string, not in the regular expression. It is also not always portable; for instance, Emacs requires escaping as \& for the character to have the special feature.

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

#44
bash associative arrays are fantastic, I've made heavy use of them, but be warned that there have been several memory leaks in the implementation. (Sorry, no version notes, once we realized this, we rewrote a key component in C.)

IIRC, the latest bash addresses all of these, but that doesn't help too much if you are stuck with an older, stable OS, e.g., RHEL 7 or 8; even 9 likely has a few remaining.

These leaks become an issue if you have a long running bash script with frequent adds and updates. The update leak can be mitigated somewhat by calling unset, e.g., unset a[b], before updating, but only partially (again, apologies, no notes, just the memory of the discovery and the need to drop bash).

I'd agree with the idea that bash wasn't the best choice for this purpose in the first place, but there was history and other technical debt involved.

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

#46
post #31
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 >

My one-shot memory worsened with years but grasping concepts from examples got much better due to experience. So I find walls of text much less useful than a continuous “synopsis” of snippets and best practices. One usage example is worth a hundred of words and few pages of it may be worth a whole manual, especially when “at hand” through editor means. For bash idk if that exists on the internet, so I maintain my own…

> One usage example is worth a hundred words ... For bash idk if that exists on the internet, ...

Totally agree with that - I maintain a big txt file too.

Maybe this bash compendium is a bit similar:

https://www.commandlinefu.com/commands/browse

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

#47
post #31
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 >

My one-shot memory worsened with years but grasping concepts from examples got much better due to experience. So I find walls of text much less useful than a continuous “synopsis” of snippets and best practices. One usage example is worth a hundred of words and few pages of it may be worth a whole manual, especially when “at hand” through editor means. For bash idk if that exists on the internet, so I maintain my own…

You might like these:

• https://learnxinyminutes.com/>

And some code search engines:

• Debian: https://codesearch.debian.net/>

• Python: https://www.programcreek.com/python/>

• Linux: https://livegrep.com/search/linux>

• GitHub: https://grep.app/>

• HTML, JS and CSS: https://publicwww.com/>

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

#48

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

Too many. I still write /bin/sh syntax (I know it's a symlink to bash now but I mean the old school sh). Anything that requires bash-that-isnt-sh is usually better written in perl or something else.

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

#49
post #19

Picky and probably pointless question: are they actually hashmaps? If I understand correctly, a hashmap isn’t the only way to implement an associative array.

This seems pertinent, NEWS > bash-5.0 to bash-5.1 > mm

Seems to suggest it's indeed a hashmap in implementation, but I can't be bothered to look any closer.

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

#50
post #43

Earlier quoted context omitted.

That's how I discovered the '&' regex feature in less some years back.

Do you mean PCRE’s “(?&name)” feature? That is not portable; for instance, Python calls it “(?P=name)”. Or do you mean the common feature of “&” in the replacement string meaning “the whole matched string”? While this feature is common in languages which use regexps (including ed, sed, awk, etc.), that’s not actually a regexp feature, since the & character has that effect in the replacement string , not in the regula…

From man(1) less:

  &pattern
       Display  only  lines which match the pattern; lines which do not
       match the pattern are not displayed.  If pattern  is  empty  (if
       you  type  &  immediately  followed  by ENTER), any filtering is
       turned off, and all lines are displayed.  While filtering is  in
       effect,  an  ampersand  is  displayed  at  the  beginning of the
       prompt, as a reminder that some lines in the file may be hidden.

  $ less --version
  less 487 (GNU regular expressions)
  Copyright (C) 1984-2016  Mark Nudelman
Post reply on HN