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
.
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
.
If you build the array in a loop, here is a simple way:
Yet another solution:
Edit: same but for multi-character variable length separator:
Shorter version of top answer:
Usage:
Here's one that I think is POSIX-compatible:
Thanks @gniourf_gniourf for detailed comments on my combination of best worlds so far. Sorry for posting code not thoroughly designed and tested. Here is a better try.
This beauty by conception is
Additional examples:
Perhaps I'm missing something obvious, since I'm a newb to the whole bash/zsh thing, but it looks to me like you don't need to use
printf
at all. Nor does it get really ugly to do without.At least, it has worked for me thus far without issue.
For instance,
join \| *.sh
, which, let's say I'm in my~
directory, outputsutilities.sh|play.sh|foobar.sh
. Good enough for me.EDIT: This is basically Nil Geisweiller's answer, but generalized into a function.