-
-
Save rm--/5bee136fb5040647e504549b134ce45f to your computer and use it in GitHub Desktop.
Looping over tuples in Bash
This file contains hidden or 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
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 |
This file contains hidden or 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
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 |
This file contains hidden or 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
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