sed not working on a variable within a bash script; requesting a file. Simple example











up vote
1
down vote

favorite












If I declare a variable within a bash script, and then try to operate on it with sed, I keep getting errors. I've tried with double quotes, back ticks and avoiding single quotes on my variable. Here is what I'm essentially doing.



Call my script with multiple parameters



./myScript.sh apples oranges ilike,apples,oranges,bananas


My objective is to use sed to replace $3 "," with " ", then use wc -w to count how many words are in $3.



MyScript.sh



fruits="$3"
checkFruits= sed -i 's/,/ /g' <<< "$fruits"
echo $checkFruits


And the result after running the script in the terminal:



ilike,apples,oranges,bananas
sed: no input files


P.s. After countless google searches, reading suggestions and playing with my code, I simply cannot get this easy sample of code to work and I'm not really sure why. And I can't try to implement the wc -w until I move past this block.










share|improve this question




























    up vote
    1
    down vote

    favorite












    If I declare a variable within a bash script, and then try to operate on it with sed, I keep getting errors. I've tried with double quotes, back ticks and avoiding single quotes on my variable. Here is what I'm essentially doing.



    Call my script with multiple parameters



    ./myScript.sh apples oranges ilike,apples,oranges,bananas


    My objective is to use sed to replace $3 "," with " ", then use wc -w to count how many words are in $3.



    MyScript.sh



    fruits="$3"
    checkFruits= sed -i 's/,/ /g' <<< "$fruits"
    echo $checkFruits


    And the result after running the script in the terminal:



    ilike,apples,oranges,bananas
    sed: no input files


    P.s. After countless google searches, reading suggestions and playing with my code, I simply cannot get this easy sample of code to work and I'm not really sure why. And I can't try to implement the wc -w until I move past this block.










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      If I declare a variable within a bash script, and then try to operate on it with sed, I keep getting errors. I've tried with double quotes, back ticks and avoiding single quotes on my variable. Here is what I'm essentially doing.



      Call my script with multiple parameters



      ./myScript.sh apples oranges ilike,apples,oranges,bananas


      My objective is to use sed to replace $3 "," with " ", then use wc -w to count how many words are in $3.



      MyScript.sh



      fruits="$3"
      checkFruits= sed -i 's/,/ /g' <<< "$fruits"
      echo $checkFruits


      And the result after running the script in the terminal:



      ilike,apples,oranges,bananas
      sed: no input files


      P.s. After countless google searches, reading suggestions and playing with my code, I simply cannot get this easy sample of code to work and I'm not really sure why. And I can't try to implement the wc -w until I move past this block.










      share|improve this question















      If I declare a variable within a bash script, and then try to operate on it with sed, I keep getting errors. I've tried with double quotes, back ticks and avoiding single quotes on my variable. Here is what I'm essentially doing.



      Call my script with multiple parameters



      ./myScript.sh apples oranges ilike,apples,oranges,bananas


      My objective is to use sed to replace $3 "," with " ", then use wc -w to count how many words are in $3.



      MyScript.sh



      fruits="$3"
      checkFruits= sed -i 's/,/ /g' <<< "$fruits"
      echo $checkFruits


      And the result after running the script in the terminal:



      ilike,apples,oranges,bananas
      sed: no input files


      P.s. After countless google searches, reading suggestions and playing with my code, I simply cannot get this easy sample of code to work and I'm not really sure why. And I can't try to implement the wc -w until I move past this block.







      linux bash variables unix sed






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 11:20









      halfer

      14.2k757106




      14.2k757106










      asked Nov 9 at 22:05









      Karim

      215




      215
























          4 Answers
          4






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          The -i flag to sed requires a file argument, without it the sed command does what you expect.



          However, I'd consider using tr instead of sed for this simple replacement:



          fruits="$3"
          checkFruits="$(tr , ' ' <<< $fruits)"
          echo $checkFruits


          Looking at the larger picture, do you want to count comma-separated strings, or the number of words once you have changed commas into spaces? For instance, do you want the string "i like,apples,oranges,and bananas" to return a count of 4, or 6? (This question is moot if you are 100% sure you will never have spaces in your input data.)



          If 6, then the other answers (including mine) will already work.



          However, if you want the answer to be 4, then you might want to do something else, like:



          fruits="$3"
          checkFruits="$(tr , \n <<< $fruits)"
          itemCount="$(wc -l <<< $checkFruits)"


          Of course this can be condensed a little, but just throwing out the question as to what you're really doing. When asking a question here, it's good to post your expected results along with the input data and the code you've already used to try to solve the problem.






          share|improve this answer























          • -i requires an extension argument only on macOS, not on Linux
            – John Weldon
            Nov 9 at 22:13










          • On Ubuntu 18.04 using -i gave the same error as OP described.
            – Jeff Breadner
            Nov 9 at 22:14












          • You're right - I think the OP intended to use -e
            – John Weldon
            Nov 9 at 22:19










          • Exactly what I needed. Thank you! Can't upvote you unfortunately.
            – Karim
            Nov 10 at 12:43






          • 1




            @Karim You should be able to accept the answer (click on the check mark beside the answer): stackoverflow.com/help/someone-answers
            – Walter A
            Nov 10 at 19:44


















          up vote
          3
          down vote













          You can do



          fruits="$3"
          checkFruits="${3//,/ }"
          # or
          echo "${3//,/ }"





          share|improve this answer

















          • 1




            I like this approach better if the OP is already using bash
            – John Weldon
            Nov 9 at 22:32






          • 1




            Yeah, I totally forgot about bash's built-in string replacement. This is superior to both sed and tr for the use case presented.
            – Jeff Breadner
            Nov 9 at 22:32


















          up vote
          1
          down vote













          The -i option is for inplace editing of input file, you don't need it here.



          To assign a command's output to a variable, use command expansion like var=$(command).



          fruits="$3"
          checkFruits=$(sed 's/,/ /g' <<< "$fruits")
          echo $checkFruits





          share|improve this answer






























            up vote
            0
            down vote













            You don't need sed at all.



            IFS=, read -a things <<< "$3"
            echo "${#things[@]}"





            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              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',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              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%2fstackoverflow.com%2fquestions%2f53233844%2fsed-not-working-on-a-variable-within-a-bash-script-requesting-a-file-simple-ex%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              1
              down vote



              accepted










              The -i flag to sed requires a file argument, without it the sed command does what you expect.



              However, I'd consider using tr instead of sed for this simple replacement:



              fruits="$3"
              checkFruits="$(tr , ' ' <<< $fruits)"
              echo $checkFruits


              Looking at the larger picture, do you want to count comma-separated strings, or the number of words once you have changed commas into spaces? For instance, do you want the string "i like,apples,oranges,and bananas" to return a count of 4, or 6? (This question is moot if you are 100% sure you will never have spaces in your input data.)



              If 6, then the other answers (including mine) will already work.



              However, if you want the answer to be 4, then you might want to do something else, like:



              fruits="$3"
              checkFruits="$(tr , \n <<< $fruits)"
              itemCount="$(wc -l <<< $checkFruits)"


              Of course this can be condensed a little, but just throwing out the question as to what you're really doing. When asking a question here, it's good to post your expected results along with the input data and the code you've already used to try to solve the problem.






              share|improve this answer























              • -i requires an extension argument only on macOS, not on Linux
                – John Weldon
                Nov 9 at 22:13










              • On Ubuntu 18.04 using -i gave the same error as OP described.
                – Jeff Breadner
                Nov 9 at 22:14












              • You're right - I think the OP intended to use -e
                – John Weldon
                Nov 9 at 22:19










              • Exactly what I needed. Thank you! Can't upvote you unfortunately.
                – Karim
                Nov 10 at 12:43






              • 1




                @Karim You should be able to accept the answer (click on the check mark beside the answer): stackoverflow.com/help/someone-answers
                – Walter A
                Nov 10 at 19:44















              up vote
              1
              down vote



              accepted










              The -i flag to sed requires a file argument, without it the sed command does what you expect.



              However, I'd consider using tr instead of sed for this simple replacement:



              fruits="$3"
              checkFruits="$(tr , ' ' <<< $fruits)"
              echo $checkFruits


              Looking at the larger picture, do you want to count comma-separated strings, or the number of words once you have changed commas into spaces? For instance, do you want the string "i like,apples,oranges,and bananas" to return a count of 4, or 6? (This question is moot if you are 100% sure you will never have spaces in your input data.)



              If 6, then the other answers (including mine) will already work.



              However, if you want the answer to be 4, then you might want to do something else, like:



              fruits="$3"
              checkFruits="$(tr , \n <<< $fruits)"
              itemCount="$(wc -l <<< $checkFruits)"


              Of course this can be condensed a little, but just throwing out the question as to what you're really doing. When asking a question here, it's good to post your expected results along with the input data and the code you've already used to try to solve the problem.






              share|improve this answer























              • -i requires an extension argument only on macOS, not on Linux
                – John Weldon
                Nov 9 at 22:13










              • On Ubuntu 18.04 using -i gave the same error as OP described.
                – Jeff Breadner
                Nov 9 at 22:14












              • You're right - I think the OP intended to use -e
                – John Weldon
                Nov 9 at 22:19










              • Exactly what I needed. Thank you! Can't upvote you unfortunately.
                – Karim
                Nov 10 at 12:43






              • 1




                @Karim You should be able to accept the answer (click on the check mark beside the answer): stackoverflow.com/help/someone-answers
                – Walter A
                Nov 10 at 19:44













              up vote
              1
              down vote



              accepted







              up vote
              1
              down vote



              accepted






              The -i flag to sed requires a file argument, without it the sed command does what you expect.



              However, I'd consider using tr instead of sed for this simple replacement:



              fruits="$3"
              checkFruits="$(tr , ' ' <<< $fruits)"
              echo $checkFruits


              Looking at the larger picture, do you want to count comma-separated strings, or the number of words once you have changed commas into spaces? For instance, do you want the string "i like,apples,oranges,and bananas" to return a count of 4, or 6? (This question is moot if you are 100% sure you will never have spaces in your input data.)



              If 6, then the other answers (including mine) will already work.



              However, if you want the answer to be 4, then you might want to do something else, like:



              fruits="$3"
              checkFruits="$(tr , \n <<< $fruits)"
              itemCount="$(wc -l <<< $checkFruits)"


              Of course this can be condensed a little, but just throwing out the question as to what you're really doing. When asking a question here, it's good to post your expected results along with the input data and the code you've already used to try to solve the problem.






              share|improve this answer














              The -i flag to sed requires a file argument, without it the sed command does what you expect.



              However, I'd consider using tr instead of sed for this simple replacement:



              fruits="$3"
              checkFruits="$(tr , ' ' <<< $fruits)"
              echo $checkFruits


              Looking at the larger picture, do you want to count comma-separated strings, or the number of words once you have changed commas into spaces? For instance, do you want the string "i like,apples,oranges,and bananas" to return a count of 4, or 6? (This question is moot if you are 100% sure you will never have spaces in your input data.)



              If 6, then the other answers (including mine) will already work.



              However, if you want the answer to be 4, then you might want to do something else, like:



              fruits="$3"
              checkFruits="$(tr , \n <<< $fruits)"
              itemCount="$(wc -l <<< $checkFruits)"


              Of course this can be condensed a little, but just throwing out the question as to what you're really doing. When asking a question here, it's good to post your expected results along with the input data and the code you've already used to try to solve the problem.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 9 at 22:29

























              answered Nov 9 at 22:11









              Jeff Breadner

              1,010617




              1,010617












              • -i requires an extension argument only on macOS, not on Linux
                – John Weldon
                Nov 9 at 22:13










              • On Ubuntu 18.04 using -i gave the same error as OP described.
                – Jeff Breadner
                Nov 9 at 22:14












              • You're right - I think the OP intended to use -e
                – John Weldon
                Nov 9 at 22:19










              • Exactly what I needed. Thank you! Can't upvote you unfortunately.
                – Karim
                Nov 10 at 12:43






              • 1




                @Karim You should be able to accept the answer (click on the check mark beside the answer): stackoverflow.com/help/someone-answers
                – Walter A
                Nov 10 at 19:44


















              • -i requires an extension argument only on macOS, not on Linux
                – John Weldon
                Nov 9 at 22:13










              • On Ubuntu 18.04 using -i gave the same error as OP described.
                – Jeff Breadner
                Nov 9 at 22:14












              • You're right - I think the OP intended to use -e
                – John Weldon
                Nov 9 at 22:19










              • Exactly what I needed. Thank you! Can't upvote you unfortunately.
                – Karim
                Nov 10 at 12:43






              • 1




                @Karim You should be able to accept the answer (click on the check mark beside the answer): stackoverflow.com/help/someone-answers
                – Walter A
                Nov 10 at 19:44
















              -i requires an extension argument only on macOS, not on Linux
              – John Weldon
              Nov 9 at 22:13




              -i requires an extension argument only on macOS, not on Linux
              – John Weldon
              Nov 9 at 22:13












              On Ubuntu 18.04 using -i gave the same error as OP described.
              – Jeff Breadner
              Nov 9 at 22:14






              On Ubuntu 18.04 using -i gave the same error as OP described.
              – Jeff Breadner
              Nov 9 at 22:14














              You're right - I think the OP intended to use -e
              – John Weldon
              Nov 9 at 22:19




              You're right - I think the OP intended to use -e
              – John Weldon
              Nov 9 at 22:19












              Exactly what I needed. Thank you! Can't upvote you unfortunately.
              – Karim
              Nov 10 at 12:43




              Exactly what I needed. Thank you! Can't upvote you unfortunately.
              – Karim
              Nov 10 at 12:43




              1




              1




              @Karim You should be able to accept the answer (click on the check mark beside the answer): stackoverflow.com/help/someone-answers
              – Walter A
              Nov 10 at 19:44




              @Karim You should be able to accept the answer (click on the check mark beside the answer): stackoverflow.com/help/someone-answers
              – Walter A
              Nov 10 at 19:44












              up vote
              3
              down vote













              You can do



              fruits="$3"
              checkFruits="${3//,/ }"
              # or
              echo "${3//,/ }"





              share|improve this answer

















              • 1




                I like this approach better if the OP is already using bash
                – John Weldon
                Nov 9 at 22:32






              • 1




                Yeah, I totally forgot about bash's built-in string replacement. This is superior to both sed and tr for the use case presented.
                – Jeff Breadner
                Nov 9 at 22:32















              up vote
              3
              down vote













              You can do



              fruits="$3"
              checkFruits="${3//,/ }"
              # or
              echo "${3//,/ }"





              share|improve this answer

















              • 1




                I like this approach better if the OP is already using bash
                – John Weldon
                Nov 9 at 22:32






              • 1




                Yeah, I totally forgot about bash's built-in string replacement. This is superior to both sed and tr for the use case presented.
                – Jeff Breadner
                Nov 9 at 22:32













              up vote
              3
              down vote










              up vote
              3
              down vote









              You can do



              fruits="$3"
              checkFruits="${3//,/ }"
              # or
              echo "${3//,/ }"





              share|improve this answer












              You can do



              fruits="$3"
              checkFruits="${3//,/ }"
              # or
              echo "${3//,/ }"






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 9 at 22:30









              Walter A

              10.1k2930




              10.1k2930








              • 1




                I like this approach better if the OP is already using bash
                – John Weldon
                Nov 9 at 22:32






              • 1




                Yeah, I totally forgot about bash's built-in string replacement. This is superior to both sed and tr for the use case presented.
                – Jeff Breadner
                Nov 9 at 22:32














              • 1




                I like this approach better if the OP is already using bash
                – John Weldon
                Nov 9 at 22:32






              • 1




                Yeah, I totally forgot about bash's built-in string replacement. This is superior to both sed and tr for the use case presented.
                – Jeff Breadner
                Nov 9 at 22:32








              1




              1




              I like this approach better if the OP is already using bash
              – John Weldon
              Nov 9 at 22:32




              I like this approach better if the OP is already using bash
              – John Weldon
              Nov 9 at 22:32




              1




              1




              Yeah, I totally forgot about bash's built-in string replacement. This is superior to both sed and tr for the use case presented.
              – Jeff Breadner
              Nov 9 at 22:32




              Yeah, I totally forgot about bash's built-in string replacement. This is superior to both sed and tr for the use case presented.
              – Jeff Breadner
              Nov 9 at 22:32










              up vote
              1
              down vote













              The -i option is for inplace editing of input file, you don't need it here.



              To assign a command's output to a variable, use command expansion like var=$(command).



              fruits="$3"
              checkFruits=$(sed 's/,/ /g' <<< "$fruits")
              echo $checkFruits





              share|improve this answer



























                up vote
                1
                down vote













                The -i option is for inplace editing of input file, you don't need it here.



                To assign a command's output to a variable, use command expansion like var=$(command).



                fruits="$3"
                checkFruits=$(sed 's/,/ /g' <<< "$fruits")
                echo $checkFruits





                share|improve this answer

























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  The -i option is for inplace editing of input file, you don't need it here.



                  To assign a command's output to a variable, use command expansion like var=$(command).



                  fruits="$3"
                  checkFruits=$(sed 's/,/ /g' <<< "$fruits")
                  echo $checkFruits





                  share|improve this answer














                  The -i option is for inplace editing of input file, you don't need it here.



                  To assign a command's output to a variable, use command expansion like var=$(command).



                  fruits="$3"
                  checkFruits=$(sed 's/,/ /g' <<< "$fruits")
                  echo $checkFruits






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 9 at 22:30

























                  answered Nov 9 at 22:10









                  oguzismail

                  2,269518




                  2,269518






















                      up vote
                      0
                      down vote













                      You don't need sed at all.



                      IFS=, read -a things <<< "$3"
                      echo "${#things[@]}"





                      share|improve this answer

























                        up vote
                        0
                        down vote













                        You don't need sed at all.



                        IFS=, read -a things <<< "$3"
                        echo "${#things[@]}"





                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          You don't need sed at all.



                          IFS=, read -a things <<< "$3"
                          echo "${#things[@]}"





                          share|improve this answer












                          You don't need sed at all.



                          IFS=, read -a things <<< "$3"
                          echo "${#things[@]}"






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 9 at 23:04









                          chepner

                          239k29228319




                          239k29228319






























                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233844%2fsed-not-working-on-a-variable-within-a-bash-script-requesting-a-file-simple-ex%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

                              Guess what letter conforming each word

                              Port of Spain

                              Run scheduled task as local user group (not BUILTIN)