I'm trying to use Bash string substitution to combine an array. Desired output:
cat,dog,fish
This works, but isn't there a way to do with without piping?
#!/bin/bash
words=(cat dog fish)
echo "${words[@]}" | sed 's/ /,/g'
I've been trying this, which doesn't work.
echo "${words[@]// /,/}"
,
. You only use spaces when filling the array with a list, to indicate to bash where one element ends and the next one starts. You could just as well have saiddeclare -A words; words[1]="cat"; words[2]="dog"; words[3]="fish"
and get the same array content. – AdminBee Aug 13 '20 at 08:23