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…
Today I learned that bash has hashmaps (2024)
101–110 of 137 posts
Re: Today I learned that bash has hashmaps (2024)
#102I 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?
Re: Today I learned that bash has hashmaps (2024)
#103Got 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.
That sounds like the wrong direction. ;)
Re: Today I learned that bash has hashmaps (2024)
#104Nice 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.
Re: Today I learned that bash has hashmaps (2024)
#105Picky and probably pointless question: are they actually hashmaps? If I understand correctly, a hashmap isn’t the only way to implement an associative array.
Re: Today I learned that bash has hashmaps (2024)
#106Earlier 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.
Re: Today I learned that bash has hashmaps (2024)
#107Earlier 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…
Re: Today I learned that bash has hashmaps (2024)
#108Earlier 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.
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)
#109Unfortunately, MacOS ships an earlier version of bash that does not include associative arrays, so they’re not as portable as you might like.
Re: Today I learned that bash has hashmaps (2024)
#110(I feel obliged to add that when you feel the need to add type annotations a bit later is when to abandon python)