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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 2 down vote accepted

Assuming that the elements do not contain spaces, you could translate spaces to commas:

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

which produces:

ab,ac,ad

You can also use ranges with characters:

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

This is especially useful if you want a larger range.

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.