Skip to content

Instantly share code, notes, and snippets.

@rm--
Forked from victorwyee/cleanest.sh
Created January 28, 2025 10:16
Show Gist options
  • Save rm--/5bee136fb5040647e504549b134ce45f to your computer and use it in GitHub Desktop.
Save rm--/5bee136fb5040647e504549b134ce45f to your computer and use it in GitHub Desktop.
Looping over tuples in Bash
declare -a elems=(
"a 1"
"b 2"
"c 3 word"
"d 4"
)
for elem in "${elems[@]}"; do
read -a strarr <<< "$elem" # uses default whitespace IFS
echo ${strarr[0]} ${strarr[1]} ${strarr[2]}
done
for elem in a,1 b,2 c,3,word d,4; do
IFS=',' read -a strarr <<< "${elem}"
echo ${strarr[0]} ${strarr[1]} ${strarr[2]}
done
OLDIFS=$IFS; IFS=',';
for elem in a,1 b,2 c,3,word d,4; do
set -- $elem
echo $1 $2 $3
done
IFS=$OLDIFS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment