Loop over tuples in bash?

后端 未结 12 1533
说谎
说谎 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:19

    I believe this solution is a little cleaner than the others that have been submitted, h/t to this bash style guide for illustrating how read can be used to split strings at a delimiter and assign them to individual variables.

    for i in c,3 e,5; do 
        IFS=',' read item1 item2 <<< "${i}"
        echo "${item1}" and "${item2}"
    done
    

提交回复
热议问题