How to get osx shell script to show colors in echo












26















I'm trying to add color output to my errors in a bash script that I have running on a mac. The problem is the colors are not working. I created the simplest of scripts to demonstrate that it does not work:



#!/bin/bash

echo -e "e[1;31m This is red text e[0m"


However, when i run it, I see no colors at all, as shown in this image. The color output of the ls command is working fine however.



enter image description here










share|improve this question























  • take a look at this, which I answered your question there: stackoverflow.com/a/48216286/3782119

    – SHi ON
    Jan 13 '18 at 23:39











  • I was happy to know that bash was old, and updating it solved my problem.

    – nycynik
    Jan 17 '18 at 13:58
















26















I'm trying to add color output to my errors in a bash script that I have running on a mac. The problem is the colors are not working. I created the simplest of scripts to demonstrate that it does not work:



#!/bin/bash

echo -e "e[1;31m This is red text e[0m"


However, when i run it, I see no colors at all, as shown in this image. The color output of the ls command is working fine however.



enter image description here










share|improve this question























  • take a look at this, which I answered your question there: stackoverflow.com/a/48216286/3782119

    – SHi ON
    Jan 13 '18 at 23:39











  • I was happy to know that bash was old, and updating it solved my problem.

    – nycynik
    Jan 17 '18 at 13:58














26












26








26


7






I'm trying to add color output to my errors in a bash script that I have running on a mac. The problem is the colors are not working. I created the simplest of scripts to demonstrate that it does not work:



#!/bin/bash

echo -e "e[1;31m This is red text e[0m"


However, when i run it, I see no colors at all, as shown in this image. The color output of the ls command is working fine however.



enter image description here










share|improve this question














I'm trying to add color output to my errors in a bash script that I have running on a mac. The problem is the colors are not working. I created the simplest of scripts to demonstrate that it does not work:



#!/bin/bash

echo -e "e[1;31m This is red text e[0m"


However, when i run it, I see no colors at all, as shown in this image. The color output of the ls command is working fine however.



enter image description here







macos bash shell echo






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Feb 28 '15 at 13:44









nycyniknycynik

5,21454572




5,21454572













  • take a look at this, which I answered your question there: stackoverflow.com/a/48216286/3782119

    – SHi ON
    Jan 13 '18 at 23:39











  • I was happy to know that bash was old, and updating it solved my problem.

    – nycynik
    Jan 17 '18 at 13:58



















  • take a look at this, which I answered your question there: stackoverflow.com/a/48216286/3782119

    – SHi ON
    Jan 13 '18 at 23:39











  • I was happy to know that bash was old, and updating it solved my problem.

    – nycynik
    Jan 17 '18 at 13:58

















take a look at this, which I answered your question there: stackoverflow.com/a/48216286/3782119

– SHi ON
Jan 13 '18 at 23:39





take a look at this, which I answered your question there: stackoverflow.com/a/48216286/3782119

– SHi ON
Jan 13 '18 at 23:39













I was happy to know that bash was old, and updating it solved my problem.

– nycynik
Jan 17 '18 at 13:58





I was happy to know that bash was old, and updating it solved my problem.

– nycynik
Jan 17 '18 at 13:58












4 Answers
4






active

oldest

votes


















27














OSX ships with an old version of Bash that does not support the e escape character. Use x1B or update Bash (brew install bash).



Even better, though, would be to use tput.






share|improve this answer
























  • Thanks! Found a better bash after installing brew at brew.sh

    – nycynik
    Feb 28 '15 at 13:58











  • /usr/local/bin/bash --version yields GNU bash, version 4.4.23(1)-release (x86_64-apple-darwin17.5.0) Copyright (C) 2016 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <gnu.org/licenses/gpl.html>

    – nycynik
    Dec 16 '18 at 2:11











  • @nycynik The shipped version of bash was different 46 months before your comment; it was still on 3.x.

    – danemacmillan
    Jan 10 at 15:00











  • My second comment was the version of bash output after upgrading it .

    – nycynik
    Jan 14 at 1:10



















55














Use 33 or x1B instead of e to represent de <Esc> character.



echo -e "33[1;31m This is red text 33[0m"


See http://misc.flogisoft.com/bash/tip_colors_and_formatting






share|improve this answer
























  • Thanks for hte link

    – nycynik
    May 22 '16 at 15:31



















3














Another option could be using zsh, which respects the e notation.



#!/bin/zsh





share|improve this answer































    1














    In script files printf could be yet another option, you have to add trailing "n" though.



    #!/bin/bash

    echo -e "e[31mOutput as is.e[m"
    printf "e[32mThis is green line.e[mn"
    printf "e[33;1m%sn" 'This is yellow bold line.'


    Tested on macOS High Sierra 10.13.6:



    % /bin/bash --version
    GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin17)
    Copyright (C) 2007 Free Software Foundation, Inc.





    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',
      autoActivateHeartbeat: false,
      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%2f28782394%2fhow-to-get-osx-shell-script-to-show-colors-in-echo%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









      27














      OSX ships with an old version of Bash that does not support the e escape character. Use x1B or update Bash (brew install bash).



      Even better, though, would be to use tput.






      share|improve this answer
























      • Thanks! Found a better bash after installing brew at brew.sh

        – nycynik
        Feb 28 '15 at 13:58











      • /usr/local/bin/bash --version yields GNU bash, version 4.4.23(1)-release (x86_64-apple-darwin17.5.0) Copyright (C) 2016 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <gnu.org/licenses/gpl.html>

        – nycynik
        Dec 16 '18 at 2:11











      • @nycynik The shipped version of bash was different 46 months before your comment; it was still on 3.x.

        – danemacmillan
        Jan 10 at 15:00











      • My second comment was the version of bash output after upgrading it .

        – nycynik
        Jan 14 at 1:10
















      27














      OSX ships with an old version of Bash that does not support the e escape character. Use x1B or update Bash (brew install bash).



      Even better, though, would be to use tput.






      share|improve this answer
























      • Thanks! Found a better bash after installing brew at brew.sh

        – nycynik
        Feb 28 '15 at 13:58











      • /usr/local/bin/bash --version yields GNU bash, version 4.4.23(1)-release (x86_64-apple-darwin17.5.0) Copyright (C) 2016 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <gnu.org/licenses/gpl.html>

        – nycynik
        Dec 16 '18 at 2:11











      • @nycynik The shipped version of bash was different 46 months before your comment; it was still on 3.x.

        – danemacmillan
        Jan 10 at 15:00











      • My second comment was the version of bash output after upgrading it .

        – nycynik
        Jan 14 at 1:10














      27












      27








      27







      OSX ships with an old version of Bash that does not support the e escape character. Use x1B or update Bash (brew install bash).



      Even better, though, would be to use tput.






      share|improve this answer













      OSX ships with an old version of Bash that does not support the e escape character. Use x1B or update Bash (brew install bash).



      Even better, though, would be to use tput.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Feb 28 '15 at 13:51









      danemacmillandanemacmillan

      881912




      881912













      • Thanks! Found a better bash after installing brew at brew.sh

        – nycynik
        Feb 28 '15 at 13:58











      • /usr/local/bin/bash --version yields GNU bash, version 4.4.23(1)-release (x86_64-apple-darwin17.5.0) Copyright (C) 2016 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <gnu.org/licenses/gpl.html>

        – nycynik
        Dec 16 '18 at 2:11











      • @nycynik The shipped version of bash was different 46 months before your comment; it was still on 3.x.

        – danemacmillan
        Jan 10 at 15:00











      • My second comment was the version of bash output after upgrading it .

        – nycynik
        Jan 14 at 1:10



















      • Thanks! Found a better bash after installing brew at brew.sh

        – nycynik
        Feb 28 '15 at 13:58











      • /usr/local/bin/bash --version yields GNU bash, version 4.4.23(1)-release (x86_64-apple-darwin17.5.0) Copyright (C) 2016 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <gnu.org/licenses/gpl.html>

        – nycynik
        Dec 16 '18 at 2:11











      • @nycynik The shipped version of bash was different 46 months before your comment; it was still on 3.x.

        – danemacmillan
        Jan 10 at 15:00











      • My second comment was the version of bash output after upgrading it .

        – nycynik
        Jan 14 at 1:10

















      Thanks! Found a better bash after installing brew at brew.sh

      – nycynik
      Feb 28 '15 at 13:58





      Thanks! Found a better bash after installing brew at brew.sh

      – nycynik
      Feb 28 '15 at 13:58













      /usr/local/bin/bash --version yields GNU bash, version 4.4.23(1)-release (x86_64-apple-darwin17.5.0) Copyright (C) 2016 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <gnu.org/licenses/gpl.html>

      – nycynik
      Dec 16 '18 at 2:11





      /usr/local/bin/bash --version yields GNU bash, version 4.4.23(1)-release (x86_64-apple-darwin17.5.0) Copyright (C) 2016 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <gnu.org/licenses/gpl.html>

      – nycynik
      Dec 16 '18 at 2:11













      @nycynik The shipped version of bash was different 46 months before your comment; it was still on 3.x.

      – danemacmillan
      Jan 10 at 15:00





      @nycynik The shipped version of bash was different 46 months before your comment; it was still on 3.x.

      – danemacmillan
      Jan 10 at 15:00













      My second comment was the version of bash output after upgrading it .

      – nycynik
      Jan 14 at 1:10





      My second comment was the version of bash output after upgrading it .

      – nycynik
      Jan 14 at 1:10













      55














      Use 33 or x1B instead of e to represent de <Esc> character.



      echo -e "33[1;31m This is red text 33[0m"


      See http://misc.flogisoft.com/bash/tip_colors_and_formatting






      share|improve this answer
























      • Thanks for hte link

        – nycynik
        May 22 '16 at 15:31
















      55














      Use 33 or x1B instead of e to represent de <Esc> character.



      echo -e "33[1;31m This is red text 33[0m"


      See http://misc.flogisoft.com/bash/tip_colors_and_formatting






      share|improve this answer
























      • Thanks for hte link

        – nycynik
        May 22 '16 at 15:31














      55












      55








      55







      Use 33 or x1B instead of e to represent de <Esc> character.



      echo -e "33[1;31m This is red text 33[0m"


      See http://misc.flogisoft.com/bash/tip_colors_and_formatting






      share|improve this answer













      Use 33 or x1B instead of e to represent de <Esc> character.



      echo -e "33[1;31m This is red text 33[0m"


      See http://misc.flogisoft.com/bash/tip_colors_and_formatting







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered May 21 '16 at 17:35









      guapologuapolo

      1,39511316




      1,39511316













      • Thanks for hte link

        – nycynik
        May 22 '16 at 15:31



















      • Thanks for hte link

        – nycynik
        May 22 '16 at 15:31

















      Thanks for hte link

      – nycynik
      May 22 '16 at 15:31





      Thanks for hte link

      – nycynik
      May 22 '16 at 15:31











      3














      Another option could be using zsh, which respects the e notation.



      #!/bin/zsh





      share|improve this answer




























        3














        Another option could be using zsh, which respects the e notation.



        #!/bin/zsh





        share|improve this answer


























          3












          3








          3







          Another option could be using zsh, which respects the e notation.



          #!/bin/zsh





          share|improve this answer













          Another option could be using zsh, which respects the e notation.



          #!/bin/zsh






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 1 '16 at 17:03









          Adam MatanAdam Matan

          50k93264419




          50k93264419























              1














              In script files printf could be yet another option, you have to add trailing "n" though.



              #!/bin/bash

              echo -e "e[31mOutput as is.e[m"
              printf "e[32mThis is green line.e[mn"
              printf "e[33;1m%sn" 'This is yellow bold line.'


              Tested on macOS High Sierra 10.13.6:



              % /bin/bash --version
              GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin17)
              Copyright (C) 2007 Free Software Foundation, Inc.





              share|improve this answer




























                1














                In script files printf could be yet another option, you have to add trailing "n" though.



                #!/bin/bash

                echo -e "e[31mOutput as is.e[m"
                printf "e[32mThis is green line.e[mn"
                printf "e[33;1m%sn" 'This is yellow bold line.'


                Tested on macOS High Sierra 10.13.6:



                % /bin/bash --version
                GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin17)
                Copyright (C) 2007 Free Software Foundation, Inc.





                share|improve this answer


























                  1












                  1








                  1







                  In script files printf could be yet another option, you have to add trailing "n" though.



                  #!/bin/bash

                  echo -e "e[31mOutput as is.e[m"
                  printf "e[32mThis is green line.e[mn"
                  printf "e[33;1m%sn" 'This is yellow bold line.'


                  Tested on macOS High Sierra 10.13.6:



                  % /bin/bash --version
                  GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin17)
                  Copyright (C) 2007 Free Software Foundation, Inc.





                  share|improve this answer













                  In script files printf could be yet another option, you have to add trailing "n" though.



                  #!/bin/bash

                  echo -e "e[31mOutput as is.e[m"
                  printf "e[32mThis is green line.e[mn"
                  printf "e[33;1m%sn" 'This is yellow bold line.'


                  Tested on macOS High Sierra 10.13.6:



                  % /bin/bash --version
                  GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin17)
                  Copyright (C) 2007 Free Software Foundation, Inc.






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 19 '18 at 13:17









                  cu39cu39

                  1112




                  1112






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • 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%2fstackoverflow.com%2fquestions%2f28782394%2fhow-to-get-osx-shell-script-to-show-colors-in-echo%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







                      這個網誌中的熱門文章

                      Tangent Lines Diagram Along Smooth Curve

                      Yusuf al-Mu'taman ibn Hud

                      Zucchini