Loop through a comma-separated shell variable

前端 未结 8 1928
醉话见心
醉话见心 2020-12-22 18:15

Suppose I have a Unix shell variable as below

variable=abc,def,ghij

I want to extract all the values (abc, def an

8条回答
  •  感情败类
    2020-12-22 18:59

    Here is an alternative tr based solution that doesn't use echo, expressed as a one-liner.

    for v in $(tr ',' '\n' <<< "$var") ; do something_with "$v" ; done
    

    It feels tidier without echo but that is just my personal preference.

提交回复
热议问题