Skip to content

Instantly share code, notes, and snippets.

@caruccio
Last active January 6, 2024 22:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caruccio/9dfe532e4d1f991d82294eaf4275f9e3 to your computer and use it in GitHub Desktop.
Save caruccio/9dfe532e4d1f991d82294eaf4275f9e3 to your computer and use it in GitHub Desktop.
Bash multi-value arrays
#
# 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