Live data from Hacker News

Today I learned that bash has hashmaps (2024)

xeiaso.net

101–110 of 137 posts

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

#101
post #68

Earlier quoted context omitted.

I'm not seeing this local scope leak with bash 5.2.15. The below script works as I'd expect: #!/bin/bash declare -A map1=([x]=2) echo "1. Global scope map1[x]: ${map1[x]}" func1() { echo " * Enter func1" local -A map1 map1[x]=3 echo " Local scope map1[x]: ${map1[x]}" } func1 echo "2. Global scope map1[x]: ${map1[x]}" outputting 1. Global scope map1[x]: 2 * Enter func1 Local scope map1[x]: 3 2. Global scope map1[x]: 2

The local scope leak seems to only happen when you drop down the call stack. See below how I can call func2 from the top level and it's fine, but if I call it from within func1, it will leak. #!/bin/bash declare -A map1=([x]=2) echo "1. Global scope map1[x]: ${map1[x]}" func1() { echo " * Enter func1" local -A map1 map1[x]=3 echo " Local scope map1[x]: ${map1[x]}" func2 } func2() { echo " * Enter func2" echo " Local…

This is know as dynamic scope, as opposed to lexical scope: https://en.wikipedia.org/wiki/Scope_(computer_science)#Lexic...

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

#102
post #18

I love shell, I think it's killer feature are pipes and whoever figured out how to design so you can pipe in and out of control structures(Doug Mcilroy?) is a goddamn genius. however, after writing one to many overly clever shell scripts, I have a very clearly delineated point in which the script has become too complex and it is time to rewrite in a language better suited for the task. and that point is when I need a…

Control structures? Do you mean something like '{ cmd1 || cmd2; } | cmd3', where the control structures are the braces?

“Control structure” normally refers to conditionals and loops. You can pipe in and out of a for loop in the shell, maybe that’s what they mean.

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

#103

Got burned on this on an interview question before. In a related question I said something about switching to a language like Python when a script started to get "complicated" Then the interviewer explained how his favorite language was bash, how he uses it for everything... I did not get the job. Ironically my next job I did a bunch of Perl to Bash conversion.

> Perl to Bash conversion

That sounds like the wrong direction. ;)

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

#104

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 find the semantics (e.g. the order in which elements are replaced/parsed/processed) more of a problem than the syntax.

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

#106
post #32

Earlier quoted context omitted.

You should be getting bash from homebrew anyways.

Or using an OS which doesn’t ship ancient software. Linux exists. It’s pretty awesome.

When they make a decent laptop for linux, maybe I'll switch. I wanted so badly to move to System76, but their screens are awful.

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

#107
post #43

Earlier quoted context omitted.

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 4…

Oh, you meant the program “less”! Right, I understand what you meant now.

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

#108
post #72

Earlier quoted context omitted.

>they have some fantastic foot guns! Otherwise wouldn't be getting the full shell experience.

Why hasn't shell scripting evolved? It's god-awful.

It's evolved quite a bit if you don't need a `sh`-compatible shell.

I'm a huge fan of `fish` for personal scripting and interactive use: https://fishshell.com/

Obviously you'll still have `bash` installed somewhere on your computer, but at least you don't have to look at it as much.

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

#109
post #56

Unfortunately, MacOS ships an earlier version of bash that does not include associative arrays, so they’re not as portable as you might like.

You can use homebrew to install the latest version of bash and then chsh to set it as your default shell. That’s what I do anyways

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

#110
When you feel the need for a hash in shell, or even an array, is also probably when you should rewrite your shell in python.

(I feel obliged to add that when you feel the need to add type annotations a bit later is when to abandon python)

Post reply on HN