If I've got an array like this in Bash:
FOO=( a b c )
How do I join the elements with commas? For example, producing a,b,c
.
If I've got an array like this in Bash:
FOO=( a b c )
How do I join the elements with commas? For example, producing a,b,c
.
Maybe, e.g.,
SAVE_IFS=$IFS
IFS=","
FOOJOIN="${FOO[*]}"
IFS=$SAVE_IFS
echo $FOOJOIN
Right now I'm using:
TO_IGNORE=(
E201 # Whitespace after '('
E301 # Expected N blank lines, found M
E303 # Too many blank lines (pep8 gets confused by comments)
)
ARGS="--ignore `echo ${TO_IGNORE[@]} | tr ' ' ','`"
Which works, but (in the general case) will break horribly if array elements have a space in them.
(For those interested, this is a wrapper script around pep8.py)
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}
Yet another solution:
#/!bin/bash
foo=('foo bar' 'foo baz' 'bar baz')
bar=$(printf ",%s" "${foo[@]}")
bar=${bar:1}
echo $bar
a=(a b c) b=(d e f) a=(`echo ${a[*]} ${b[*]}`) echo ${a[*]} a b c d e f