Comments (3)
👍 @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.
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.
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)
- Could you please give more detail about pipe?
- Consider including read command to prompt user to accept input from terminal
- Translation Request: Spanish
- Translation Request: Spanish HOT 1
- Translation Request: Indonesia HOT 1
- Translation Request: Japanese HOT 1
- Translation Request: [German]] HOT 1
- New Command: Read HOT 1
- Translation Request: Persian(Farsi)
- enhancement HOT 2
- Translation Request: [Please enter language here] german
- Use stable serverfault.com link for square bracket comparison link
- Include "invalid" branch in select example
- Thanks for providing a comprehensive and systematic learning sharing
- Translation Request: Russian HOT 2
- Translation Request: [French] HOT 1
- Translation Request: Japanese HOT 3
- Translation Request: [russia]
- Translation Request: [Bahasa Indonesia]
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bash-handbook.