How can I join elements of an array in Bash?

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

    This approach takes care of spaces within the values, but requires a loop:

    #!/bin/bash
    
    FOO=( a b c )
    BAR=""
    
    for index in ${!FOO[*]}
    do
        BAR="$BAR,${FOO[$index]}"
    done
    echo ${BAR:1}
    

提交回复
热议问题