I need to do IFS=",";echo {1..5} so that it can output 1,2,3,4,5 instead of 1 2 3 4 5. How do I make bash echo {1..5} and output the values with a comma?


With Bash's builtins:

This is a bit ugly since we need to separate the 5 to avoid a trailing comma:

$ printf '%s,' {1..4}; echo 5

Though since printf can output directly to a variable, that can be worked around and the final comma removed with a parameter expansion:

$ printf -v tmpvar "%s," {1..5}; echo "${tmpvar%,}"

Or with "$*", which joins using the first character of IFS. This trashes global state, but you could rather easily avoid that by running it in a subshell or in a function with local IFS:

$ IFS=,; set -- {1..5}; echo "$*";

If the limits are in variables, it's probably easiest to just do it manually with a loop since you can't use variables as endpoints in a brace expansion range. Again, the upper limit is in a special case:

a=1; b=5
for (( i=a ; i<b ; i++ )); do
    printf "$i,";
printf "$b\n" 
  • In case of variables I think a good way shall be: a=1;b=5; seq --separator="," $a $b – Sir Jo Black Dec 14 '18 at 14:36
  • 1
    @SirJoBlack, note the sentence at the very beginning of the answer: "With Bash's builtins" – ilkkachu Dec 14 '18 at 14:38

Try to use:

seq --separator="," 1 5

If you allow for spaces along with commas, try

$ echo {1..5},
1, 2, 3, 4, 5,

Since you're using numerical brace expansion, the only spaces that will ever appear are the ones between numbers, so you could post-process the result:

output=$(echo {1..5} | tr ' ' ',')


output=$(echo {1..5} | sed 's/ /,/g')

This is more to further expand on expansion than to provide a practical solution: what follows is surely less concise/efficient that the alternatives proposed in other answers.

In bash, The Internal Field Separator (IFS) is not used for brace expansion (so, IFS=","; echo {1..5} won't work as you would like).
It is used, however, when expanding a few other things. A complete list being far beyond my level of competence, here are a couple of examples that use brace expansion and , as the value of IFS to create the output you are looking for:

Positional parameters

About the expansion of the special parameter *, quoting man bash,

When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable.

We can use a function to exemplify:

$ function fnc () {
>   IFS=','
>   echo "$*"
> }
$ fnc {1..5}


When accessing the items of an array using the * subscript, again, quoting man bash,

If the word is double-quoted, ${name[*]} expands to a single word with the value of each array member separated by the first character of the IFS special variable, ...

An example:

$ arr=( {1..5} )      # Populate an array using brace expansion and compound assignment
$ IFS=','
$ echo "${arr[*]}"    # Reference all the items with the * subscript

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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