How can I expand with brackets but use commas instead of spaces as the separators in the output?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












1














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?










share|improve this question




























    1














    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?










    share|improve this question


























      1












      1








      1







      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?










      share|improve this question















      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?







      bash-expansion






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 13 at 20:37









      Jeff Schaller

      38.5k1053125




      38.5k1053125










      asked Dec 13 at 10:19









      Bret Joseph

      758




      758




















          5 Answers
          5






          active

          oldest

          votes


















          5














          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
          1,2,3,4,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%,"
          1,2,3,4,5


          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 "$*";
          1,2,3,4,5


          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,";
          done;
          printf "$bn"





          share|improve this answer






















          • In case of variables I think a good way shall be: a=1;b=5; seq --separator="," $a $b
            – Sir Jo Black
            Dec 14 at 14:36






          • 1




            @SirJoBlack, note the sentence at the very beginning of the answer: "With Bash's builtins"
            – ilkkachu
            Dec 14 at 14:38


















          3














          Try to use:



          seq --separator="," 1 5





          share|improve this answer






























            2














            If you allow for spaces along with commas, try



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





            share|improve this answer




























              2














              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 ' ' ',')


              or



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





              share|improve this answer




























                2
















                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
                1,2,3,4,5


                Arrays



                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
                1,2,3,4,5





                share|improve this answer






















                  Your Answer








                  StackExchange.ready(function()
                  var channelOptions =
                  tags: "".split(" "),
                  id: "106"
                  ;
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function()
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled)
                  StackExchange.using("snippets", function()
                  createEditor();
                  );

                  else
                  createEditor();

                  );

                  function createEditor()
                  StackExchange.prepareEditor(
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: false,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: null,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader:
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  ,
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  );



                  );













                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function ()
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f487730%2fhow-can-i-expand-with-brackets-but-use-commas-instead-of-spaces-as-the-separator%23new-answer', 'question_page');

                  );

                  Post as a guest















                  Required, but never shown

























                  5 Answers
                  5






                  active

                  oldest

                  votes








                  5 Answers
                  5






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  5














                  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
                  1,2,3,4,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%,"
                  1,2,3,4,5


                  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 "$*";
                  1,2,3,4,5


                  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,";
                  done;
                  printf "$bn"





                  share|improve this answer






















                  • In case of variables I think a good way shall be: a=1;b=5; seq --separator="," $a $b
                    – Sir Jo Black
                    Dec 14 at 14:36






                  • 1




                    @SirJoBlack, note the sentence at the very beginning of the answer: "With Bash's builtins"
                    – ilkkachu
                    Dec 14 at 14:38















                  5














                  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
                  1,2,3,4,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%,"
                  1,2,3,4,5


                  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 "$*";
                  1,2,3,4,5


                  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,";
                  done;
                  printf "$bn"





                  share|improve this answer






















                  • In case of variables I think a good way shall be: a=1;b=5; seq --separator="," $a $b
                    – Sir Jo Black
                    Dec 14 at 14:36






                  • 1




                    @SirJoBlack, note the sentence at the very beginning of the answer: "With Bash's builtins"
                    – ilkkachu
                    Dec 14 at 14:38













                  5












                  5








                  5






                  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
                  1,2,3,4,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%,"
                  1,2,3,4,5


                  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 "$*";
                  1,2,3,4,5


                  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,";
                  done;
                  printf "$bn"





                  share|improve this answer














                  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
                  1,2,3,4,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%,"
                  1,2,3,4,5


                  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 "$*";
                  1,2,3,4,5


                  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,";
                  done;
                  printf "$bn"






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Dec 14 at 9:58

























                  answered Dec 13 at 20:09









                  ilkkachu

                  55.5k783151




                  55.5k783151











                  • In case of variables I think a good way shall be: a=1;b=5; seq --separator="," $a $b
                    – Sir Jo Black
                    Dec 14 at 14:36






                  • 1




                    @SirJoBlack, note the sentence at the very beginning of the answer: "With Bash's builtins"
                    – ilkkachu
                    Dec 14 at 14:38
















                  • In case of variables I think a good way shall be: a=1;b=5; seq --separator="," $a $b
                    – Sir Jo Black
                    Dec 14 at 14:36






                  • 1




                    @SirJoBlack, note the sentence at the very beginning of the answer: "With Bash's builtins"
                    – ilkkachu
                    Dec 14 at 14:38















                  In case of variables I think a good way shall be: a=1;b=5; seq --separator="," $a $b
                  – Sir Jo Black
                  Dec 14 at 14:36




                  In case of variables I think a good way shall be: a=1;b=5; seq --separator="," $a $b
                  – Sir Jo Black
                  Dec 14 at 14:36




                  1




                  1




                  @SirJoBlack, note the sentence at the very beginning of the answer: "With Bash's builtins"
                  – ilkkachu
                  Dec 14 at 14:38




                  @SirJoBlack, note the sentence at the very beginning of the answer: "With Bash's builtins"
                  – ilkkachu
                  Dec 14 at 14:38













                  3














                  Try to use:



                  seq --separator="," 1 5





                  share|improve this answer



























                    3














                    Try to use:



                    seq --separator="," 1 5





                    share|improve this answer

























                      3












                      3








                      3






                      Try to use:



                      seq --separator="," 1 5





                      share|improve this answer














                      Try to use:



                      seq --separator="," 1 5






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Dec 13 at 10:57









                      Jeff Schaller

                      38.5k1053125




                      38.5k1053125










                      answered Dec 13 at 10:30









                      Sir Jo Black

                      1515




                      1515





















                          2














                          If you allow for spaces along with commas, try



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





                          share|improve this answer

























                            2














                            If you allow for spaces along with commas, try



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





                            share|improve this answer























                              2












                              2








                              2






                              If you allow for spaces along with commas, try



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





                              share|improve this answer












                              If you allow for spaces along with commas, try



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






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Dec 13 at 19:51









                              RudiC

                              4,1091312




                              4,1091312





















                                  2














                                  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 ' ' ',')


                                  or



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





                                  share|improve this answer

























                                    2














                                    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 ' ' ',')


                                    or



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





                                    share|improve this answer























                                      2












                                      2








                                      2






                                      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 ' ' ',')


                                      or



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





                                      share|improve this answer












                                      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 ' ' ',')


                                      or



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






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Dec 13 at 20:35









                                      Jeff Schaller

                                      38.5k1053125




                                      38.5k1053125





















                                          2
















                                          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
                                          1,2,3,4,5


                                          Arrays



                                          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
                                          1,2,3,4,5





                                          share|improve this answer



























                                            2
















                                            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
                                            1,2,3,4,5


                                            Arrays



                                            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
                                            1,2,3,4,5





                                            share|improve this answer

























                                              2












                                              2








                                              2








                                              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
                                              1,2,3,4,5


                                              Arrays



                                              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
                                              1,2,3,4,5





                                              share|improve this answer
















                                              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
                                              1,2,3,4,5


                                              Arrays



                                              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
                                              1,2,3,4,5






                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited yesterday

























                                              answered Dec 13 at 20:55









                                              fra-san

                                              1,180214




                                              1,180214



























                                                  draft saved

                                                  draft discarded
















































                                                  Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid


                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.

                                                  To learn more, see our tips on writing great answers.





                                                  Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                  Please pay close attention to the following guidance:


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid


                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.

                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function ()
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f487730%2fhow-can-i-expand-with-brackets-but-use-commas-instead-of-spaces-as-the-separator%23new-answer', 'question_page');

                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown






                                                  Popular posts from this blog

                                                  How to check contact read email or not when send email to Individual?

                                                  Bahrain

                                                  Postfix configuration issue with fips on centos 7; mailgun relay