Last active
January 6, 2024 22:30
-
-
Save caruccio/9dfe532e4d1f991d82294eaf4275f9e3 to your computer and use it in GitHub Desktop.
Bash multi-value arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Given an array of multi-value items, transform each value into a new array replacing its | |
# separator for a space, then access each individual inner-value by index: | |
# | |
ITEMS=( | |
cup:blue:3 | |
phone:black:15 | |
pen:red:1 | |
) | |
for item in "${ITEMS[@]}"; do | |
item=(${item//:/ }) # this is the trick | |
name=${item[0]} | |
color=${item[1]} | |
value=${item[2]} | |
echo "$name, $color, \$$value" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment