Git Product home page Git Product logo

Comments (3)

sumbach avatar sumbach commented on May 18, 2024

👍 @jmorcar

readarray (aka, mapfile) was added in Bash 4, so I would present this as an alternative to traditional read tricks when Bourne sh or POSIX compatibility isn't a concern. More details and examples in BashFAQ/005.

Associative arrays were added in Bash 4, also. More details and examples, including workarounds for older shells and Bash versions in BashFAQ/006. I'd appreciate if any introduction to associative arrays could mention any complications related to whitespace in and quoting of keys--this tends to trip people up.

from bash-handbook.

jmorcar avatar jmorcar commented on May 18, 2024

Okis your give another idea, Indicate historical data about bash 4 with these references or external references to complete the schema for mid/advanced users. Thanks for the reply only I suggested because are functions very useful to create arrays more near to new high level languages.

from bash-handbook.

terminalforlife avatar terminalforlife commented on May 18, 2024

I'd appreciate if any introduction to associative arrays could mention any complications related to whitespace in and quoting of keys--this tends to trip people up.

I'm late, but I'd love to weigh in here.

I use associative arrays a lot in BASH. The subscript can be stubborn, but quote-protecting it can sometimes solve the issue. When nothing else works, you could reverse the keys and values or just use regular arrays but with a designated separator between the key and value for each element. The latter often winds up being the best solution, in my experience, because it's more performant and much easier to work with.

An example of the latter solution:

Users=()
while IFS=':' read User _ UserID _; do
    Users+=("$User|$UID")
done < /etc/passwd

for Index in "${Users[@]}"; {
    printf '%s %d\n' ${Index%%|*} "${Index##*|}"
}

It's a contrived example, since it can all easily be done within the while loop without the array, but it does demonstrate what I mean.

To conclude, the best method I have for getting around this limitation of BASH is to just use regular arrays which act like an associative array. In-fact, this sort of approach can lead to some cool results, like something akin to multidimensional arrays.

Here's a great example from one from my programs, CSi3 (cheat sheet for i3WM), of combining a key=value system into an associative array:

https://github.com/terminalforlife/Extra/blob/master/source/csi3/csi3#L140-L235

Although I've just spotted something inefficient: I've repeated the declare builtin many times, which is a pretty bad oversight. I'll have to sort that. Anyway, point being, something very similar can be done with standard arrays to smash a key=value together into the value for the elements. You can also do something similar with a for loop, when an array isn't even needed, like:

for Str in Key1=Value1 Key2=Value2 ...; {
    printf '%s=%s\n' "${Str%%=*}" "${Str#*=}"
}

With this approach, it's important not to greedily match on the value side, else you might remove data from it, whereas you likely won't have the separator as or as part of the key, since that would be silly.

from bash-handbook.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.