Loop over tuples in bash?

后端 未结 12 1534
说谎
说谎 2020-12-04 21:00

Is it possible to loop over tuples in bash?

As an example, it would be great if the following worked:

for (i,j) in ((c,3), (e,5)); do echo \"$i and $         


        
12条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-04 21:10

    In cases where my tuple definitions are more complex, I prefer to have them in a heredoc:

    while IFS=", " read -ra arr; do
      echo "${arr[0]} and ${arr[1]}"
    done <

    This combines looping over lines of a heredoc with splitting the lines at some desired separating character.

提交回复
热议问题