How can I join elements of an array in Bash?

前端 未结 30 2626
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 12:05

If I have an array like this in Bash:

FOO=( a b c )

How do I join the elements with commas? For example, producing a,b,c.

30条回答
  •  清歌不尽
    2020-11-22 12:33

    I would echo the array as a string, then transform the spaces into line feeds, and then use paste to join everything in one line like so:

    tr " " "\n" <<< "$FOO" | paste -sd , -

    Results:

    a,b,c

    This seems to be the quickest and cleanest to me !

提交回复
热议问题