sub-shell inside sed

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











up vote
1
down vote

favorite












I can append to the start of a file fine with:



sed -i '1s/^/wordn/' file


I'm reading that if I use double quotes I can expand variables, so I try:



sed -i "1s/^/$(printenv)n/" file


I end up getting back:



sed: -e expression #1, char 15: unterminated `s' command


What is happening here. Is it related to the contents of the variable or something else?







share|improve this question






















  • $(command) is not a variable...
    – don_crissti
    Oct 14 '17 at 15:27














up vote
1
down vote

favorite












I can append to the start of a file fine with:



sed -i '1s/^/wordn/' file


I'm reading that if I use double quotes I can expand variables, so I try:



sed -i "1s/^/$(printenv)n/" file


I end up getting back:



sed: -e expression #1, char 15: unterminated `s' command


What is happening here. Is it related to the contents of the variable or something else?







share|improve this question






















  • $(command) is not a variable...
    – don_crissti
    Oct 14 '17 at 15:27












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I can append to the start of a file fine with:



sed -i '1s/^/wordn/' file


I'm reading that if I use double quotes I can expand variables, so I try:



sed -i "1s/^/$(printenv)n/" file


I end up getting back:



sed: -e expression #1, char 15: unterminated `s' command


What is happening here. Is it related to the contents of the variable or something else?







share|improve this question














I can append to the start of a file fine with:



sed -i '1s/^/wordn/' file


I'm reading that if I use double quotes I can expand variables, so I try:



sed -i "1s/^/$(printenv)n/" file


I end up getting back:



sed: -e expression #1, char 15: unterminated `s' command


What is happening here. Is it related to the contents of the variable or something else?









share|improve this question













share|improve this question




share|improve this question








edited Oct 14 '17 at 15:26









don_crissti

47k15124154




47k15124154










asked Oct 14 '17 at 14:56









Philip Kirkbride

2,3022470




2,3022470











  • $(command) is not a variable...
    – don_crissti
    Oct 14 '17 at 15:27
















  • $(command) is not a variable...
    – don_crissti
    Oct 14 '17 at 15:27















$(command) is not a variable...
– don_crissti
Oct 14 '17 at 15:27




$(command) is not a variable...
– don_crissti
Oct 14 '17 at 15:27










2 Answers
2






active

oldest

votes

















up vote
6
down vote



accepted










I think the following would work:



sed -i '1 e printenv' file


From the GNU sed manual:



'e COMMAND'
Executes COMMAND and sends its output to the output stream. The
command can run across multiple lines, all but the last ending with
a back-slash.


Alternatively, you can use cat, but this requires creating a temporary file:



cat <(printenv) file > temporary_file; mv temporary_file file


If the package moreutils is installed on your machine, you can avoid creating a temporary file manually by using sponge:



cat <(printenv) file | sponge file





share|improve this answer





























    up vote
    5
    down vote













    To insert the contents before line 1:



    ed -s file <<< $'0r !printenvnwq'


    To insert the contents after line 1:



    ed -s file <<< $'1r !printenvnwq'





    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',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      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%2f398118%2fsub-shell-inside-sed%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      6
      down vote



      accepted










      I think the following would work:



      sed -i '1 e printenv' file


      From the GNU sed manual:



      'e COMMAND'
      Executes COMMAND and sends its output to the output stream. The
      command can run across multiple lines, all but the last ending with
      a back-slash.


      Alternatively, you can use cat, but this requires creating a temporary file:



      cat <(printenv) file > temporary_file; mv temporary_file file


      If the package moreutils is installed on your machine, you can avoid creating a temporary file manually by using sponge:



      cat <(printenv) file | sponge file





      share|improve this answer


























        up vote
        6
        down vote



        accepted










        I think the following would work:



        sed -i '1 e printenv' file


        From the GNU sed manual:



        'e COMMAND'
        Executes COMMAND and sends its output to the output stream. The
        command can run across multiple lines, all but the last ending with
        a back-slash.


        Alternatively, you can use cat, but this requires creating a temporary file:



        cat <(printenv) file > temporary_file; mv temporary_file file


        If the package moreutils is installed on your machine, you can avoid creating a temporary file manually by using sponge:



        cat <(printenv) file | sponge file





        share|improve this answer
























          up vote
          6
          down vote



          accepted







          up vote
          6
          down vote



          accepted






          I think the following would work:



          sed -i '1 e printenv' file


          From the GNU sed manual:



          'e COMMAND'
          Executes COMMAND and sends its output to the output stream. The
          command can run across multiple lines, all but the last ending with
          a back-slash.


          Alternatively, you can use cat, but this requires creating a temporary file:



          cat <(printenv) file > temporary_file; mv temporary_file file


          If the package moreutils is installed on your machine, you can avoid creating a temporary file manually by using sponge:



          cat <(printenv) file | sponge file





          share|improve this answer














          I think the following would work:



          sed -i '1 e printenv' file


          From the GNU sed manual:



          'e COMMAND'
          Executes COMMAND and sends its output to the output stream. The
          command can run across multiple lines, all but the last ending with
          a back-slash.


          Alternatively, you can use cat, but this requires creating a temporary file:



          cat <(printenv) file > temporary_file; mv temporary_file file


          If the package moreutils is installed on your machine, you can avoid creating a temporary file manually by using sponge:



          cat <(printenv) file | sponge file






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Oct 14 '17 at 16:58

























          answered Oct 14 '17 at 15:11









          Rastapopoulos

          518112




          518112






















              up vote
              5
              down vote













              To insert the contents before line 1:



              ed -s file <<< $'0r !printenvnwq'


              To insert the contents after line 1:



              ed -s file <<< $'1r !printenvnwq'





              share|improve this answer
























                up vote
                5
                down vote













                To insert the contents before line 1:



                ed -s file <<< $'0r !printenvnwq'


                To insert the contents after line 1:



                ed -s file <<< $'1r !printenvnwq'





                share|improve this answer






















                  up vote
                  5
                  down vote










                  up vote
                  5
                  down vote









                  To insert the contents before line 1:



                  ed -s file <<< $'0r !printenvnwq'


                  To insert the contents after line 1:



                  ed -s file <<< $'1r !printenvnwq'





                  share|improve this answer












                  To insert the contents before line 1:



                  ed -s file <<< $'0r !printenvnwq'


                  To insert the contents after line 1:



                  ed -s file <<< $'1r !printenvnwq'






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Oct 14 '17 at 15:12









                  Jeff Schaller

                  32.1k849109




                  32.1k849109



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f398118%2fsub-shell-inside-sed%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

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

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?