Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'd like

$ echo a{b,c,d}

to output a comma-separated list like

ab,ac,ad

instead of the usual output

ab ac ad

What's the easiest way to do that on the command line?

share|improve this question

3 Answers 3

up vote 1 down vote accepted

Assuming that the elements do not contain spaces, you could replace , with a space:

echo a{b,c,d} | tr ' ' ,

which produces:

ab,ac,ad
share|improve this answer

It seems bash does not use $IFS to join the generated words. Another technique would be to store the generated words in an array and then $IFS will be in play:

I'm going to use a subshell so I don't alter this shell's IFS: pick one of

( words=( a{b,c,d} ); IFS=,; echo "${words[*]}" )
( set -- a{b,c,d}; IFS=,; echo "$*" )

That emits the comma-separates string to stdout. If you want to capture it:

joined=$( set -- a{b,c,d}; IFS=,; echo "$*" )
share|improve this answer

I am sure there are many ways to accomplish this. Here is one method:

echo a{b,c,d} | sed 's/ /,/g'
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.