reset the color of the output after my command in bash shell

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











up vote
1
down vote

favorite












I want to distinguish my command from the rest of the output of the shell easily through different colors. But I don't have much experience with customizing my bash shell, so I don't know how to reset the color (after) my input.



my current PS1 variable looks like this:



export PS1="$redu$green$(__git_ps1) $turkw
$white$ "

# '$(__git_ps1)' git status prompt (generates a space before it even if empty)


So my input is white. But even the output of the commands is white because it is not reset. Furthermore, if the command itself color codes its output, then it itself resets the colors which result in some ugly mixing of white and gray.



So how do I reset the color after my input command?







share|improve this question
























    up vote
    1
    down vote

    favorite












    I want to distinguish my command from the rest of the output of the shell easily through different colors. But I don't have much experience with customizing my bash shell, so I don't know how to reset the color (after) my input.



    my current PS1 variable looks like this:



    export PS1="$redu$green$(__git_ps1) $turkw
    $white$ "

    # '$(__git_ps1)' git status prompt (generates a space before it even if empty)


    So my input is white. But even the output of the commands is white because it is not reset. Furthermore, if the command itself color codes its output, then it itself resets the colors which result in some ugly mixing of white and gray.



    So how do I reset the color after my input command?







    share|improve this question






















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I want to distinguish my command from the rest of the output of the shell easily through different colors. But I don't have much experience with customizing my bash shell, so I don't know how to reset the color (after) my input.



      my current PS1 variable looks like this:



      export PS1="$redu$green$(__git_ps1) $turkw
      $white$ "

      # '$(__git_ps1)' git status prompt (generates a space before it even if empty)


      So my input is white. But even the output of the commands is white because it is not reset. Furthermore, if the command itself color codes its output, then it itself resets the colors which result in some ugly mixing of white and gray.



      So how do I reset the color after my input command?







      share|improve this question












      I want to distinguish my command from the rest of the output of the shell easily through different colors. But I don't have much experience with customizing my bash shell, so I don't know how to reset the color (after) my input.



      my current PS1 variable looks like this:



      export PS1="$redu$green$(__git_ps1) $turkw
      $white$ "

      # '$(__git_ps1)' git status prompt (generates a space before it even if empty)


      So my input is white. But even the output of the commands is white because it is not reset. Furthermore, if the command itself color codes its output, then it itself resets the colors which result in some ugly mixing of white and gray.



      So how do I reset the color after my input command?









      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 25 '17 at 7:08









      Lockon2000

      82




      82




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          The DEBUG trap runs before each command, so you can abuse it to reset the colors, if you have colored the command line input. ("abuse" since this isn't debugging.)



          With this:



          $ promptcol="$(tput sgr0)$(tput setaf 3)"
          $ cmdcol="$(tput sgr0)$(tput bold)"
          $ normalcol="$(tput sgr0)"
          $ trap 'echo -n "$normalcol"' DEBUG
          $ PS1="[$promptcol]w$ [$cmdcol]"


          I get this:



          colored prompt



          Note that you need the [...] surrounding the color codes, so that the shell knows how to calculate the width of the prompt properly. Also, note that you can't put the [...] within the variables, the shell handles the prompt escapes first, and variable expansions only after that.






          share|improve this answer




















          • Okay, if I understand your answer correctly, you say that with (trap '.....' DEBUG) I can add personalized commands to be executed whenever the DEBUG trap runs (without changing its former code, I am just adding to it). So by adding something like this (trap 'echo -n -e "e[0m"' DEBUG) in my .bash_profile (before the PS1 variable) I should get the desired behavior. Okay, I tried it out and it worked. But did I really understand everything correctly?? And thanks for your help :)
            – Lockon2000
            Dec 26 '17 at 17:13










          • Ah, also I amended my PS1 variable as you suggested. Now it looks like this PS1="[$red]u[$green]$(__git_ps1) [$turk]w [$white]$ "
            – Lockon2000
            Dec 26 '17 at 17:16

















          up vote
          1
          down vote













          On the ~/.bash_logout Insert the following:



          echo -e "33[0m"
          /usr/bin/clear





          share|improve this answer




















          • maybe you didn't get what I want correctly. What I want is somthing to change back the colors (reset all attributes) after (entering) each and every command, not after finishing my entire session.
            – Lockon2000
            Dec 26 '17 at 17:00











          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%2f412900%2freset-the-color-of-the-output-after-my-command-in-bash-shell%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
          2
          down vote



          accepted










          The DEBUG trap runs before each command, so you can abuse it to reset the colors, if you have colored the command line input. ("abuse" since this isn't debugging.)



          With this:



          $ promptcol="$(tput sgr0)$(tput setaf 3)"
          $ cmdcol="$(tput sgr0)$(tput bold)"
          $ normalcol="$(tput sgr0)"
          $ trap 'echo -n "$normalcol"' DEBUG
          $ PS1="[$promptcol]w$ [$cmdcol]"


          I get this:



          colored prompt



          Note that you need the [...] surrounding the color codes, so that the shell knows how to calculate the width of the prompt properly. Also, note that you can't put the [...] within the variables, the shell handles the prompt escapes first, and variable expansions only after that.






          share|improve this answer




















          • Okay, if I understand your answer correctly, you say that with (trap '.....' DEBUG) I can add personalized commands to be executed whenever the DEBUG trap runs (without changing its former code, I am just adding to it). So by adding something like this (trap 'echo -n -e "e[0m"' DEBUG) in my .bash_profile (before the PS1 variable) I should get the desired behavior. Okay, I tried it out and it worked. But did I really understand everything correctly?? And thanks for your help :)
            – Lockon2000
            Dec 26 '17 at 17:13










          • Ah, also I amended my PS1 variable as you suggested. Now it looks like this PS1="[$red]u[$green]$(__git_ps1) [$turk]w [$white]$ "
            – Lockon2000
            Dec 26 '17 at 17:16














          up vote
          2
          down vote



          accepted










          The DEBUG trap runs before each command, so you can abuse it to reset the colors, if you have colored the command line input. ("abuse" since this isn't debugging.)



          With this:



          $ promptcol="$(tput sgr0)$(tput setaf 3)"
          $ cmdcol="$(tput sgr0)$(tput bold)"
          $ normalcol="$(tput sgr0)"
          $ trap 'echo -n "$normalcol"' DEBUG
          $ PS1="[$promptcol]w$ [$cmdcol]"


          I get this:



          colored prompt



          Note that you need the [...] surrounding the color codes, so that the shell knows how to calculate the width of the prompt properly. Also, note that you can't put the [...] within the variables, the shell handles the prompt escapes first, and variable expansions only after that.






          share|improve this answer




















          • Okay, if I understand your answer correctly, you say that with (trap '.....' DEBUG) I can add personalized commands to be executed whenever the DEBUG trap runs (without changing its former code, I am just adding to it). So by adding something like this (trap 'echo -n -e "e[0m"' DEBUG) in my .bash_profile (before the PS1 variable) I should get the desired behavior. Okay, I tried it out and it worked. But did I really understand everything correctly?? And thanks for your help :)
            – Lockon2000
            Dec 26 '17 at 17:13










          • Ah, also I amended my PS1 variable as you suggested. Now it looks like this PS1="[$red]u[$green]$(__git_ps1) [$turk]w [$white]$ "
            – Lockon2000
            Dec 26 '17 at 17:16












          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          The DEBUG trap runs before each command, so you can abuse it to reset the colors, if you have colored the command line input. ("abuse" since this isn't debugging.)



          With this:



          $ promptcol="$(tput sgr0)$(tput setaf 3)"
          $ cmdcol="$(tput sgr0)$(tput bold)"
          $ normalcol="$(tput sgr0)"
          $ trap 'echo -n "$normalcol"' DEBUG
          $ PS1="[$promptcol]w$ [$cmdcol]"


          I get this:



          colored prompt



          Note that you need the [...] surrounding the color codes, so that the shell knows how to calculate the width of the prompt properly. Also, note that you can't put the [...] within the variables, the shell handles the prompt escapes first, and variable expansions only after that.






          share|improve this answer












          The DEBUG trap runs before each command, so you can abuse it to reset the colors, if you have colored the command line input. ("abuse" since this isn't debugging.)



          With this:



          $ promptcol="$(tput sgr0)$(tput setaf 3)"
          $ cmdcol="$(tput sgr0)$(tput bold)"
          $ normalcol="$(tput sgr0)"
          $ trap 'echo -n "$normalcol"' DEBUG
          $ PS1="[$promptcol]w$ [$cmdcol]"


          I get this:



          colored prompt



          Note that you need the [...] surrounding the color codes, so that the shell knows how to calculate the width of the prompt properly. Also, note that you can't put the [...] within the variables, the shell handles the prompt escapes first, and variable expansions only after that.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 25 '17 at 10:32









          ilkkachu

          49.9k674137




          49.9k674137











          • Okay, if I understand your answer correctly, you say that with (trap '.....' DEBUG) I can add personalized commands to be executed whenever the DEBUG trap runs (without changing its former code, I am just adding to it). So by adding something like this (trap 'echo -n -e "e[0m"' DEBUG) in my .bash_profile (before the PS1 variable) I should get the desired behavior. Okay, I tried it out and it worked. But did I really understand everything correctly?? And thanks for your help :)
            – Lockon2000
            Dec 26 '17 at 17:13










          • Ah, also I amended my PS1 variable as you suggested. Now it looks like this PS1="[$red]u[$green]$(__git_ps1) [$turk]w [$white]$ "
            – Lockon2000
            Dec 26 '17 at 17:16
















          • Okay, if I understand your answer correctly, you say that with (trap '.....' DEBUG) I can add personalized commands to be executed whenever the DEBUG trap runs (without changing its former code, I am just adding to it). So by adding something like this (trap 'echo -n -e "e[0m"' DEBUG) in my .bash_profile (before the PS1 variable) I should get the desired behavior. Okay, I tried it out and it worked. But did I really understand everything correctly?? And thanks for your help :)
            – Lockon2000
            Dec 26 '17 at 17:13










          • Ah, also I amended my PS1 variable as you suggested. Now it looks like this PS1="[$red]u[$green]$(__git_ps1) [$turk]w [$white]$ "
            – Lockon2000
            Dec 26 '17 at 17:16















          Okay, if I understand your answer correctly, you say that with (trap '.....' DEBUG) I can add personalized commands to be executed whenever the DEBUG trap runs (without changing its former code, I am just adding to it). So by adding something like this (trap 'echo -n -e "e[0m"' DEBUG) in my .bash_profile (before the PS1 variable) I should get the desired behavior. Okay, I tried it out and it worked. But did I really understand everything correctly?? And thanks for your help :)
          – Lockon2000
          Dec 26 '17 at 17:13




          Okay, if I understand your answer correctly, you say that with (trap '.....' DEBUG) I can add personalized commands to be executed whenever the DEBUG trap runs (without changing its former code, I am just adding to it). So by adding something like this (trap 'echo -n -e "e[0m"' DEBUG) in my .bash_profile (before the PS1 variable) I should get the desired behavior. Okay, I tried it out and it worked. But did I really understand everything correctly?? And thanks for your help :)
          – Lockon2000
          Dec 26 '17 at 17:13












          Ah, also I amended my PS1 variable as you suggested. Now it looks like this PS1="[$red]u[$green]$(__git_ps1) [$turk]w [$white]$ "
          – Lockon2000
          Dec 26 '17 at 17:16




          Ah, also I amended my PS1 variable as you suggested. Now it looks like this PS1="[$red]u[$green]$(__git_ps1) [$turk]w [$white]$ "
          – Lockon2000
          Dec 26 '17 at 17:16












          up vote
          1
          down vote













          On the ~/.bash_logout Insert the following:



          echo -e "33[0m"
          /usr/bin/clear





          share|improve this answer




















          • maybe you didn't get what I want correctly. What I want is somthing to change back the colors (reset all attributes) after (entering) each and every command, not after finishing my entire session.
            – Lockon2000
            Dec 26 '17 at 17:00















          up vote
          1
          down vote













          On the ~/.bash_logout Insert the following:



          echo -e "33[0m"
          /usr/bin/clear





          share|improve this answer




















          • maybe you didn't get what I want correctly. What I want is somthing to change back the colors (reset all attributes) after (entering) each and every command, not after finishing my entire session.
            – Lockon2000
            Dec 26 '17 at 17:00













          up vote
          1
          down vote










          up vote
          1
          down vote









          On the ~/.bash_logout Insert the following:



          echo -e "33[0m"
          /usr/bin/clear





          share|improve this answer












          On the ~/.bash_logout Insert the following:



          echo -e "33[0m"
          /usr/bin/clear






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 25 '17 at 7:24









          αԋɱҽԃ αмєяιcαη

          407418




          407418











          • maybe you didn't get what I want correctly. What I want is somthing to change back the colors (reset all attributes) after (entering) each and every command, not after finishing my entire session.
            – Lockon2000
            Dec 26 '17 at 17:00

















          • maybe you didn't get what I want correctly. What I want is somthing to change back the colors (reset all attributes) after (entering) each and every command, not after finishing my entire session.
            – Lockon2000
            Dec 26 '17 at 17:00
















          maybe you didn't get what I want correctly. What I want is somthing to change back the colors (reset all attributes) after (entering) each and every command, not after finishing my entire session.
          – Lockon2000
          Dec 26 '17 at 17:00





          maybe you didn't get what I want correctly. What I want is somthing to change back the colors (reset all attributes) after (entering) each and every command, not after finishing my entire session.
          – Lockon2000
          Dec 26 '17 at 17:00













           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f412900%2freset-the-color-of-the-output-after-my-command-in-bash-shell%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?