How can I use sed to edit bash debug output? (bash -x)

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











up vote
2
down vote

favorite












#!/bin/bash -x
echo This is a script that has debugging turned on


This script outputs



+ echo This is a script that has debugging turned on
This is a script that has debugging turned on


I want to get rid of these +'s by deleting them or replacing them. I expected sed could fix my problem (sed 's/^++//g') -- But this approach doesn't affect the debug output lines.



With some more experimenting, I discovered that the debug output seems to be getting written to stderr (inferred this with the command ./test.sh 2>/dev/null which the output then excludes the debug lines)



With this new information, I would expect this to work
./test.sh 2>&1 | sed 's/^++//g'



But, alas, I still get the same undesired output:



+ echo This is a script that has debugging turned on
This is a script that has debugging turned on









share|improve this question







New contributor




aitee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 2




    Why are you working so hard to undo what -x has added? Why not just remove the -x?
    – Jeff Schaller
    1 hour ago














up vote
2
down vote

favorite












#!/bin/bash -x
echo This is a script that has debugging turned on


This script outputs



+ echo This is a script that has debugging turned on
This is a script that has debugging turned on


I want to get rid of these +'s by deleting them or replacing them. I expected sed could fix my problem (sed 's/^++//g') -- But this approach doesn't affect the debug output lines.



With some more experimenting, I discovered that the debug output seems to be getting written to stderr (inferred this with the command ./test.sh 2>/dev/null which the output then excludes the debug lines)



With this new information, I would expect this to work
./test.sh 2>&1 | sed 's/^++//g'



But, alas, I still get the same undesired output:



+ echo This is a script that has debugging turned on
This is a script that has debugging turned on









share|improve this question







New contributor




aitee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 2




    Why are you working so hard to undo what -x has added? Why not just remove the -x?
    – Jeff Schaller
    1 hour ago












up vote
2
down vote

favorite









up vote
2
down vote

favorite











#!/bin/bash -x
echo This is a script that has debugging turned on


This script outputs



+ echo This is a script that has debugging turned on
This is a script that has debugging turned on


I want to get rid of these +'s by deleting them or replacing them. I expected sed could fix my problem (sed 's/^++//g') -- But this approach doesn't affect the debug output lines.



With some more experimenting, I discovered that the debug output seems to be getting written to stderr (inferred this with the command ./test.sh 2>/dev/null which the output then excludes the debug lines)



With this new information, I would expect this to work
./test.sh 2>&1 | sed 's/^++//g'



But, alas, I still get the same undesired output:



+ echo This is a script that has debugging turned on
This is a script that has debugging turned on









share|improve this question







New contributor




aitee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











#!/bin/bash -x
echo This is a script that has debugging turned on


This script outputs



+ echo This is a script that has debugging turned on
This is a script that has debugging turned on


I want to get rid of these +'s by deleting them or replacing them. I expected sed could fix my problem (sed 's/^++//g') -- But this approach doesn't affect the debug output lines.



With some more experimenting, I discovered that the debug output seems to be getting written to stderr (inferred this with the command ./test.sh 2>/dev/null which the output then excludes the debug lines)



With this new information, I would expect this to work
./test.sh 2>&1 | sed 's/^++//g'



But, alas, I still get the same undesired output:



+ echo This is a script that has debugging turned on
This is a script that has debugging turned on






bash sed scripting stderr






share|improve this question







New contributor




aitee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




aitee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




aitee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 1 hour ago









aitee

111




111




New contributor




aitee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





aitee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






aitee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 2




    Why are you working so hard to undo what -x has added? Why not just remove the -x?
    – Jeff Schaller
    1 hour ago












  • 2




    Why are you working so hard to undo what -x has added? Why not just remove the -x?
    – Jeff Schaller
    1 hour ago







2




2




Why are you working so hard to undo what -x has added? Why not just remove the -x?
– Jeff Schaller
1 hour ago




Why are you working so hard to undo what -x has added? Why not just remove the -x?
– Jeff Schaller
1 hour ago










3 Answers
3






active

oldest

votes

















up vote
3
down vote













The + is the PS4 prompt. Set it to an empty string:



#!/bin/bash

PS4=''
set -x

echo 'This is a script that has debugging turned on'


Testing:



$ bash script.sh
echo 'This is a script that has debugging turned on'
This is a script that has debugging turned on


Or, with your original script, set PS4 to an empty string for the script when invoking it:



$ PS4='' ./script.sh
echo This is a script that has debugging turned on
This is a script that has debugging turned on


This could be used to insert a timestamp:



$ PS4='$(date +"%T: ")' ./script.sh
21:08:19: echo 'This is a script that has debugging turned on'
This is a script that has debugging turned on
21:08:19: echo 'Now sleeping for 2 seconds'
Now sleeping for 2 seconds
21:08:19: sleep 2
21:08:21: echo Done
Done





share|improve this answer





























    up vote
    2
    down vote













    The main limitation you're running into is that + is an extended regular expression feature, so you'll need to enable extended regular expression functionality; with most seds, that's with the -E flag:



    ./test.sh 2>&1 | sed -E 's/^++ //'


    I made two other changes:



    • added a trailing space, so that debugged commands show up left-aligned

    • removed the /g flag, since the regex is anchored, there can be only one match per line





    share|improve this answer






















    • Note that once you commingle stdout and stderr, any actual (stdout) output with leading plus signs will also be stripped.
      – Jeff Schaller
      1 hour ago

















    up vote
    1
    down vote













    You can redirect stderr into a process substitution. However that may affect the order of the output:



    $ bash -x -c 'hostname; echo "$(date)"'
    + hostname
    jackmanVM
    ++ date
    + echo 'Tue Oct 23 15:22:02 EDT 2018'
    Tue Oct 23 15:22:02 EDT 2018

    $ bash -x -c 'hostname; echo "$(date)"' 2> >(sed -E 's/^++/debug: /')
    debug: hostname
    jackmanVM
    debug: date
    Tue Oct 23 15:22:35 EDT 2018
    debug: echo 'Tue Oct 23 15:22:35 EDT 2018'





    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
      );



      );






      aitee is a new contributor. Be nice, and check out our Code of Conduct.









       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f477330%2fhow-can-i-use-sed-to-edit-bash-debug-output-bash-x%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote













      The + is the PS4 prompt. Set it to an empty string:



      #!/bin/bash

      PS4=''
      set -x

      echo 'This is a script that has debugging turned on'


      Testing:



      $ bash script.sh
      echo 'This is a script that has debugging turned on'
      This is a script that has debugging turned on


      Or, with your original script, set PS4 to an empty string for the script when invoking it:



      $ PS4='' ./script.sh
      echo This is a script that has debugging turned on
      This is a script that has debugging turned on


      This could be used to insert a timestamp:



      $ PS4='$(date +"%T: ")' ./script.sh
      21:08:19: echo 'This is a script that has debugging turned on'
      This is a script that has debugging turned on
      21:08:19: echo 'Now sleeping for 2 seconds'
      Now sleeping for 2 seconds
      21:08:19: sleep 2
      21:08:21: echo Done
      Done





      share|improve this answer


























        up vote
        3
        down vote













        The + is the PS4 prompt. Set it to an empty string:



        #!/bin/bash

        PS4=''
        set -x

        echo 'This is a script that has debugging turned on'


        Testing:



        $ bash script.sh
        echo 'This is a script that has debugging turned on'
        This is a script that has debugging turned on


        Or, with your original script, set PS4 to an empty string for the script when invoking it:



        $ PS4='' ./script.sh
        echo This is a script that has debugging turned on
        This is a script that has debugging turned on


        This could be used to insert a timestamp:



        $ PS4='$(date +"%T: ")' ./script.sh
        21:08:19: echo 'This is a script that has debugging turned on'
        This is a script that has debugging turned on
        21:08:19: echo 'Now sleeping for 2 seconds'
        Now sleeping for 2 seconds
        21:08:19: sleep 2
        21:08:21: echo Done
        Done





        share|improve this answer
























          up vote
          3
          down vote










          up vote
          3
          down vote









          The + is the PS4 prompt. Set it to an empty string:



          #!/bin/bash

          PS4=''
          set -x

          echo 'This is a script that has debugging turned on'


          Testing:



          $ bash script.sh
          echo 'This is a script that has debugging turned on'
          This is a script that has debugging turned on


          Or, with your original script, set PS4 to an empty string for the script when invoking it:



          $ PS4='' ./script.sh
          echo This is a script that has debugging turned on
          This is a script that has debugging turned on


          This could be used to insert a timestamp:



          $ PS4='$(date +"%T: ")' ./script.sh
          21:08:19: echo 'This is a script that has debugging turned on'
          This is a script that has debugging turned on
          21:08:19: echo 'Now sleeping for 2 seconds'
          Now sleeping for 2 seconds
          21:08:19: sleep 2
          21:08:21: echo Done
          Done





          share|improve this answer














          The + is the PS4 prompt. Set it to an empty string:



          #!/bin/bash

          PS4=''
          set -x

          echo 'This is a script that has debugging turned on'


          Testing:



          $ bash script.sh
          echo 'This is a script that has debugging turned on'
          This is a script that has debugging turned on


          Or, with your original script, set PS4 to an empty string for the script when invoking it:



          $ PS4='' ./script.sh
          echo This is a script that has debugging turned on
          This is a script that has debugging turned on


          This could be used to insert a timestamp:



          $ PS4='$(date +"%T: ")' ./script.sh
          21:08:19: echo 'This is a script that has debugging turned on'
          This is a script that has debugging turned on
          21:08:19: echo 'Now sleeping for 2 seconds'
          Now sleeping for 2 seconds
          21:08:19: sleep 2
          21:08:21: echo Done
          Done






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 32 mins ago

























          answered 38 mins ago









          Kusalananda

          111k15216341




          111k15216341






















              up vote
              2
              down vote













              The main limitation you're running into is that + is an extended regular expression feature, so you'll need to enable extended regular expression functionality; with most seds, that's with the -E flag:



              ./test.sh 2>&1 | sed -E 's/^++ //'


              I made two other changes:



              • added a trailing space, so that debugged commands show up left-aligned

              • removed the /g flag, since the regex is anchored, there can be only one match per line





              share|improve this answer






















              • Note that once you commingle stdout and stderr, any actual (stdout) output with leading plus signs will also be stripped.
                – Jeff Schaller
                1 hour ago














              up vote
              2
              down vote













              The main limitation you're running into is that + is an extended regular expression feature, so you'll need to enable extended regular expression functionality; with most seds, that's with the -E flag:



              ./test.sh 2>&1 | sed -E 's/^++ //'


              I made two other changes:



              • added a trailing space, so that debugged commands show up left-aligned

              • removed the /g flag, since the regex is anchored, there can be only one match per line





              share|improve this answer






















              • Note that once you commingle stdout and stderr, any actual (stdout) output with leading plus signs will also be stripped.
                – Jeff Schaller
                1 hour ago












              up vote
              2
              down vote










              up vote
              2
              down vote









              The main limitation you're running into is that + is an extended regular expression feature, so you'll need to enable extended regular expression functionality; with most seds, that's with the -E flag:



              ./test.sh 2>&1 | sed -E 's/^++ //'


              I made two other changes:



              • added a trailing space, so that debugged commands show up left-aligned

              • removed the /g flag, since the regex is anchored, there can be only one match per line





              share|improve this answer














              The main limitation you're running into is that + is an extended regular expression feature, so you'll need to enable extended regular expression functionality; with most seds, that's with the -E flag:



              ./test.sh 2>&1 | sed -E 's/^++ //'


              I made two other changes:



              • added a trailing space, so that debugged commands show up left-aligned

              • removed the /g flag, since the regex is anchored, there can be only one match per line






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 1 hour ago









              don_crissti

              48.1k15127157




              48.1k15127157










              answered 1 hour ago









              Jeff Schaller

              34.3k951114




              34.3k951114











              • Note that once you commingle stdout and stderr, any actual (stdout) output with leading plus signs will also be stripped.
                – Jeff Schaller
                1 hour ago
















              • Note that once you commingle stdout and stderr, any actual (stdout) output with leading plus signs will also be stripped.
                – Jeff Schaller
                1 hour ago















              Note that once you commingle stdout and stderr, any actual (stdout) output with leading plus signs will also be stripped.
              – Jeff Schaller
              1 hour ago




              Note that once you commingle stdout and stderr, any actual (stdout) output with leading plus signs will also be stripped.
              – Jeff Schaller
              1 hour ago










              up vote
              1
              down vote













              You can redirect stderr into a process substitution. However that may affect the order of the output:



              $ bash -x -c 'hostname; echo "$(date)"'
              + hostname
              jackmanVM
              ++ date
              + echo 'Tue Oct 23 15:22:02 EDT 2018'
              Tue Oct 23 15:22:02 EDT 2018

              $ bash -x -c 'hostname; echo "$(date)"' 2> >(sed -E 's/^++/debug: /')
              debug: hostname
              jackmanVM
              debug: date
              Tue Oct 23 15:22:35 EDT 2018
              debug: echo 'Tue Oct 23 15:22:35 EDT 2018'





              share|improve this answer
























                up vote
                1
                down vote













                You can redirect stderr into a process substitution. However that may affect the order of the output:



                $ bash -x -c 'hostname; echo "$(date)"'
                + hostname
                jackmanVM
                ++ date
                + echo 'Tue Oct 23 15:22:02 EDT 2018'
                Tue Oct 23 15:22:02 EDT 2018

                $ bash -x -c 'hostname; echo "$(date)"' 2> >(sed -E 's/^++/debug: /')
                debug: hostname
                jackmanVM
                debug: date
                Tue Oct 23 15:22:35 EDT 2018
                debug: echo 'Tue Oct 23 15:22:35 EDT 2018'





                share|improve this answer






















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  You can redirect stderr into a process substitution. However that may affect the order of the output:



                  $ bash -x -c 'hostname; echo "$(date)"'
                  + hostname
                  jackmanVM
                  ++ date
                  + echo 'Tue Oct 23 15:22:02 EDT 2018'
                  Tue Oct 23 15:22:02 EDT 2018

                  $ bash -x -c 'hostname; echo "$(date)"' 2> >(sed -E 's/^++/debug: /')
                  debug: hostname
                  jackmanVM
                  debug: date
                  Tue Oct 23 15:22:35 EDT 2018
                  debug: echo 'Tue Oct 23 15:22:35 EDT 2018'





                  share|improve this answer












                  You can redirect stderr into a process substitution. However that may affect the order of the output:



                  $ bash -x -c 'hostname; echo "$(date)"'
                  + hostname
                  jackmanVM
                  ++ date
                  + echo 'Tue Oct 23 15:22:02 EDT 2018'
                  Tue Oct 23 15:22:02 EDT 2018

                  $ bash -x -c 'hostname; echo "$(date)"' 2> >(sed -E 's/^++/debug: /')
                  debug: hostname
                  jackmanVM
                  debug: date
                  Tue Oct 23 15:22:35 EDT 2018
                  debug: echo 'Tue Oct 23 15:22:35 EDT 2018'






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 20 mins ago









                  glenn jackman

                  49.2k469106




                  49.2k469106




















                      aitee is a new contributor. Be nice, and check out our Code of Conduct.









                       

                      draft saved


                      draft discarded


















                      aitee is a new contributor. Be nice, and check out our Code of Conduct.












                      aitee is a new contributor. Be nice, and check out our Code of Conduct.











                      aitee is a new contributor. Be nice, and check out our Code of Conduct.













                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f477330%2fhow-can-i-use-sed-to-edit-bash-debug-output-bash-x%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Comments

                      Popular posts from this blog

                      Long meetings (6-7 hours a day): Being “babysat” by supervisor

                      Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                      Confectionery