How can I join elements of an array in Bash?

前端 未结 30 2133
爱一瞬间的悲伤
爱一瞬间的悲伤 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:31

    Surprisingly my solution is not yet given :) This is the simplest way for me. It doesn't need a function:

    IFS=, eval 'joined="${foo[*]}"'
    

    Note: This solution was observed to work well in non-POSIX mode. In POSIX mode, the elements are still joined properly, but IFS=, becomes permanent.

提交回复
热议问题