How to loop through a comma separated variable in bash

To loop through a comma separated variable in bash, need cut the elements first and then iterate over each.

Sample snippet is sharing below.

variable=text1,text2,text3
for i in $(echo $variable | sed "s/,/ /g")
do
    echo "$i"
done

This will print each element of variable one per line and we can use for any operation instead of echo here.

That's all…

Leave a Reply