Display file with ANSI colors

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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








9















I have a file with ANSI colors.



test.txt:
e[0;31mExamplee[0m



I would like to display the content of this file in a terminal, like cat does, but I would like to display the colors as well.










share|improve this question



















  • 3





    Using less -R, for instance.

    – Thomas Dickey
    Feb 14 '16 at 11:43











  • What does your cat print?

    – techraf
    Feb 14 '16 at 11:57






  • 1





    OP's question had too many possible answers to be interesting. Start with echo -e $(cat test.txt).

    – Thomas Dickey
    Feb 14 '16 at 16:31

















9















I have a file with ANSI colors.



test.txt:
e[0;31mExamplee[0m



I would like to display the content of this file in a terminal, like cat does, but I would like to display the colors as well.










share|improve this question



















  • 3





    Using less -R, for instance.

    – Thomas Dickey
    Feb 14 '16 at 11:43











  • What does your cat print?

    – techraf
    Feb 14 '16 at 11:57






  • 1





    OP's question had too many possible answers to be interesting. Start with echo -e $(cat test.txt).

    – Thomas Dickey
    Feb 14 '16 at 16:31













9












9








9


1






I have a file with ANSI colors.



test.txt:
e[0;31mExamplee[0m



I would like to display the content of this file in a terminal, like cat does, but I would like to display the colors as well.










share|improve this question
















I have a file with ANSI colors.



test.txt:
e[0;31mExamplee[0m



I would like to display the content of this file in a terminal, like cat does, but I would like to display the colors as well.







bash terminal






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 14 '16 at 13:27









Anthon

61.7k17107171




61.7k17107171










asked Feb 14 '16 at 11:32









bob dylanbob dylan

5041617




5041617







  • 3





    Using less -R, for instance.

    – Thomas Dickey
    Feb 14 '16 at 11:43











  • What does your cat print?

    – techraf
    Feb 14 '16 at 11:57






  • 1





    OP's question had too many possible answers to be interesting. Start with echo -e $(cat test.txt).

    – Thomas Dickey
    Feb 14 '16 at 16:31












  • 3





    Using less -R, for instance.

    – Thomas Dickey
    Feb 14 '16 at 11:43











  • What does your cat print?

    – techraf
    Feb 14 '16 at 11:57






  • 1





    OP's question had too many possible answers to be interesting. Start with echo -e $(cat test.txt).

    – Thomas Dickey
    Feb 14 '16 at 16:31







3




3





Using less -R, for instance.

– Thomas Dickey
Feb 14 '16 at 11:43





Using less -R, for instance.

– Thomas Dickey
Feb 14 '16 at 11:43













What does your cat print?

– techraf
Feb 14 '16 at 11:57





What does your cat print?

– techraf
Feb 14 '16 at 11:57




1




1





OP's question had too many possible answers to be interesting. Start with echo -e $(cat test.txt).

– Thomas Dickey
Feb 14 '16 at 16:31





OP's question had too many possible answers to be interesting. Start with echo -e $(cat test.txt).

– Thomas Dickey
Feb 14 '16 at 16:31










3 Answers
3






active

oldest

votes


















6














I was looking for a solution to this exact bash question. I nearly missed @Thomas Dickey's comment which provided me with the most elegant solution.



echo -e $(cat test.txt)


Some things which did not work for me are(apparently you cant pipe things to echo)



cat test.txt | echo -e


or



less -R test.txt


Another issue I had was that echo -e didn't print newlines and contiguous whitespaces within the file nicely. To print those, I used the following.



echo -ne $(cat test.txt | sed 's/$/\n/' | sed 's/ /\a /g')


This works for a test.txt file containing



e[0;31mExa mplee[0m
e[0;31mExample line2e[0m





share|improve this answer

























  • Good answer, but a rings the bell for me. I changed your second sed command to sed 's/ /\033\a /g' which doesn't ring the bell.

    – hawk
    Nov 14 '18 at 3:04







  • 1





    After further testing and finding more inconsistencies in different terminal programs (iTerm, Atom/nuclide), I've found it best to just wrap $(cat file.txt) in double quotes and add a newline at the end – no need for sed – so: echo -ne "$(<file.txt)" \n or echo -ne "$(cat <<"EOF" . . . EOF)" \n. That also prevents bash from performing glob expansion in the text.

    – hawk
    Nov 14 '18 at 21:40



















1














If you're not seeing color from cat the control characters are probably not intact.



Some tools strip out control characters but leave in the tail end.



Compare:



 echo -e "e[0;31mExamplee[0m foo"


to



 echo -e "[0;31mExample[0m foo" 


You might be able to rebuild the control sequence from what's left, though it's not fool proof as the regex you use might accidentally pull in unintended character sequences, etc. But for example:



echo -e "[0;31mExample[0m foo" | sed "s:[([0-9]*[;m]):^[[1:g"


would restore the color to the example string.






share|improve this answer
































    -1














    It should work by default.
    E.g. if I do ls --color=always > /tmp/a and than cat /tmp/a, I see the colors. Checking with od confirms that the file uses ANSI colors.



    So I think you should check if your terminal supports ANSI colors (and they are enabled).






    share|improve this answer


















    • 1





      This did not answer the question.

      – Thomas Dickey
      Feb 14 '16 at 16:23






    • 1





      No. less -R actually resolved the issue for me.

      – Ken Ingram
      Jan 1 at 2:25











    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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    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%2funix.stackexchange.com%2fquestions%2f262185%2fdisplay-file-with-ansi-colors%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    I was looking for a solution to this exact bash question. I nearly missed @Thomas Dickey's comment which provided me with the most elegant solution.



    echo -e $(cat test.txt)


    Some things which did not work for me are(apparently you cant pipe things to echo)



    cat test.txt | echo -e


    or



    less -R test.txt


    Another issue I had was that echo -e didn't print newlines and contiguous whitespaces within the file nicely. To print those, I used the following.



    echo -ne $(cat test.txt | sed 's/$/\n/' | sed 's/ /\a /g')


    This works for a test.txt file containing



    e[0;31mExa mplee[0m
    e[0;31mExample line2e[0m





    share|improve this answer

























    • Good answer, but a rings the bell for me. I changed your second sed command to sed 's/ /\033\a /g' which doesn't ring the bell.

      – hawk
      Nov 14 '18 at 3:04







    • 1





      After further testing and finding more inconsistencies in different terminal programs (iTerm, Atom/nuclide), I've found it best to just wrap $(cat file.txt) in double quotes and add a newline at the end – no need for sed – so: echo -ne "$(<file.txt)" \n or echo -ne "$(cat <<"EOF" . . . EOF)" \n. That also prevents bash from performing glob expansion in the text.

      – hawk
      Nov 14 '18 at 21:40
















    6














    I was looking for a solution to this exact bash question. I nearly missed @Thomas Dickey's comment which provided me with the most elegant solution.



    echo -e $(cat test.txt)


    Some things which did not work for me are(apparently you cant pipe things to echo)



    cat test.txt | echo -e


    or



    less -R test.txt


    Another issue I had was that echo -e didn't print newlines and contiguous whitespaces within the file nicely. To print those, I used the following.



    echo -ne $(cat test.txt | sed 's/$/\n/' | sed 's/ /\a /g')


    This works for a test.txt file containing



    e[0;31mExa mplee[0m
    e[0;31mExample line2e[0m





    share|improve this answer

























    • Good answer, but a rings the bell for me. I changed your second sed command to sed 's/ /\033\a /g' which doesn't ring the bell.

      – hawk
      Nov 14 '18 at 3:04







    • 1





      After further testing and finding more inconsistencies in different terminal programs (iTerm, Atom/nuclide), I've found it best to just wrap $(cat file.txt) in double quotes and add a newline at the end – no need for sed – so: echo -ne "$(<file.txt)" \n or echo -ne "$(cat <<"EOF" . . . EOF)" \n. That also prevents bash from performing glob expansion in the text.

      – hawk
      Nov 14 '18 at 21:40














    6












    6








    6







    I was looking for a solution to this exact bash question. I nearly missed @Thomas Dickey's comment which provided me with the most elegant solution.



    echo -e $(cat test.txt)


    Some things which did not work for me are(apparently you cant pipe things to echo)



    cat test.txt | echo -e


    or



    less -R test.txt


    Another issue I had was that echo -e didn't print newlines and contiguous whitespaces within the file nicely. To print those, I used the following.



    echo -ne $(cat test.txt | sed 's/$/\n/' | sed 's/ /\a /g')


    This works for a test.txt file containing



    e[0;31mExa mplee[0m
    e[0;31mExample line2e[0m





    share|improve this answer















    I was looking for a solution to this exact bash question. I nearly missed @Thomas Dickey's comment which provided me with the most elegant solution.



    echo -e $(cat test.txt)


    Some things which did not work for me are(apparently you cant pipe things to echo)



    cat test.txt | echo -e


    or



    less -R test.txt


    Another issue I had was that echo -e didn't print newlines and contiguous whitespaces within the file nicely. To print those, I used the following.



    echo -ne $(cat test.txt | sed 's/$/\n/' | sed 's/ /\a /g')


    This works for a test.txt file containing



    e[0;31mExa mplee[0m
    e[0;31mExample line2e[0m






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Aug 20 '18 at 22:08









    Rui F Ribeiro

    42.1k1483142




    42.1k1483142










    answered Aug 28 '17 at 5:17









    FearFear

    6113




    6113












    • Good answer, but a rings the bell for me. I changed your second sed command to sed 's/ /\033\a /g' which doesn't ring the bell.

      – hawk
      Nov 14 '18 at 3:04







    • 1





      After further testing and finding more inconsistencies in different terminal programs (iTerm, Atom/nuclide), I've found it best to just wrap $(cat file.txt) in double quotes and add a newline at the end – no need for sed – so: echo -ne "$(<file.txt)" \n or echo -ne "$(cat <<"EOF" . . . EOF)" \n. That also prevents bash from performing glob expansion in the text.

      – hawk
      Nov 14 '18 at 21:40


















    • Good answer, but a rings the bell for me. I changed your second sed command to sed 's/ /\033\a /g' which doesn't ring the bell.

      – hawk
      Nov 14 '18 at 3:04







    • 1





      After further testing and finding more inconsistencies in different terminal programs (iTerm, Atom/nuclide), I've found it best to just wrap $(cat file.txt) in double quotes and add a newline at the end – no need for sed – so: echo -ne "$(<file.txt)" \n or echo -ne "$(cat <<"EOF" . . . EOF)" \n. That also prevents bash from performing glob expansion in the text.

      – hawk
      Nov 14 '18 at 21:40

















    Good answer, but a rings the bell for me. I changed your second sed command to sed 's/ /\033\a /g' which doesn't ring the bell.

    – hawk
    Nov 14 '18 at 3:04






    Good answer, but a rings the bell for me. I changed your second sed command to sed 's/ /\033\a /g' which doesn't ring the bell.

    – hawk
    Nov 14 '18 at 3:04





    1




    1





    After further testing and finding more inconsistencies in different terminal programs (iTerm, Atom/nuclide), I've found it best to just wrap $(cat file.txt) in double quotes and add a newline at the end – no need for sed – so: echo -ne "$(<file.txt)" \n or echo -ne "$(cat <<"EOF" . . . EOF)" \n. That also prevents bash from performing glob expansion in the text.

    – hawk
    Nov 14 '18 at 21:40






    After further testing and finding more inconsistencies in different terminal programs (iTerm, Atom/nuclide), I've found it best to just wrap $(cat file.txt) in double quotes and add a newline at the end – no need for sed – so: echo -ne "$(<file.txt)" \n or echo -ne "$(cat <<"EOF" . . . EOF)" \n. That also prevents bash from performing glob expansion in the text.

    – hawk
    Nov 14 '18 at 21:40














    1














    If you're not seeing color from cat the control characters are probably not intact.



    Some tools strip out control characters but leave in the tail end.



    Compare:



     echo -e "e[0;31mExamplee[0m foo"


    to



     echo -e "[0;31mExample[0m foo" 


    You might be able to rebuild the control sequence from what's left, though it's not fool proof as the regex you use might accidentally pull in unintended character sequences, etc. But for example:



    echo -e "[0;31mExample[0m foo" | sed "s:[([0-9]*[;m]):^[[1:g"


    would restore the color to the example string.






    share|improve this answer





























      1














      If you're not seeing color from cat the control characters are probably not intact.



      Some tools strip out control characters but leave in the tail end.



      Compare:



       echo -e "e[0;31mExamplee[0m foo"


      to



       echo -e "[0;31mExample[0m foo" 


      You might be able to rebuild the control sequence from what's left, though it's not fool proof as the regex you use might accidentally pull in unintended character sequences, etc. But for example:



      echo -e "[0;31mExample[0m foo" | sed "s:[([0-9]*[;m]):^[[1:g"


      would restore the color to the example string.






      share|improve this answer



























        1












        1








        1







        If you're not seeing color from cat the control characters are probably not intact.



        Some tools strip out control characters but leave in the tail end.



        Compare:



         echo -e "e[0;31mExamplee[0m foo"


        to



         echo -e "[0;31mExample[0m foo" 


        You might be able to rebuild the control sequence from what's left, though it's not fool proof as the regex you use might accidentally pull in unintended character sequences, etc. But for example:



        echo -e "[0;31mExample[0m foo" | sed "s:[([0-9]*[;m]):^[[1:g"


        would restore the color to the example string.






        share|improve this answer















        If you're not seeing color from cat the control characters are probably not intact.



        Some tools strip out control characters but leave in the tail end.



        Compare:



         echo -e "e[0;31mExamplee[0m foo"


        to



         echo -e "[0;31mExample[0m foo" 


        You might be able to rebuild the control sequence from what's left, though it's not fool proof as the regex you use might accidentally pull in unintended character sequences, etc. But for example:



        echo -e "[0;31mExample[0m foo" | sed "s:[([0-9]*[;m]):^[[1:g"


        would restore the color to the example string.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Apr 3 '17 at 22:59

























        answered Apr 3 '17 at 18:56









        CatskulCatskul

        815816




        815816





















            -1














            It should work by default.
            E.g. if I do ls --color=always > /tmp/a and than cat /tmp/a, I see the colors. Checking with od confirms that the file uses ANSI colors.



            So I think you should check if your terminal supports ANSI colors (and they are enabled).






            share|improve this answer


















            • 1





              This did not answer the question.

              – Thomas Dickey
              Feb 14 '16 at 16:23






            • 1





              No. less -R actually resolved the issue for me.

              – Ken Ingram
              Jan 1 at 2:25















            -1














            It should work by default.
            E.g. if I do ls --color=always > /tmp/a and than cat /tmp/a, I see the colors. Checking with od confirms that the file uses ANSI colors.



            So I think you should check if your terminal supports ANSI colors (and they are enabled).






            share|improve this answer


















            • 1





              This did not answer the question.

              – Thomas Dickey
              Feb 14 '16 at 16:23






            • 1





              No. less -R actually resolved the issue for me.

              – Ken Ingram
              Jan 1 at 2:25













            -1












            -1








            -1







            It should work by default.
            E.g. if I do ls --color=always > /tmp/a and than cat /tmp/a, I see the colors. Checking with od confirms that the file uses ANSI colors.



            So I think you should check if your terminal supports ANSI colors (and they are enabled).






            share|improve this answer













            It should work by default.
            E.g. if I do ls --color=always > /tmp/a and than cat /tmp/a, I see the colors. Checking with od confirms that the file uses ANSI colors.



            So I think you should check if your terminal supports ANSI colors (and they are enabled).







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Feb 14 '16 at 11:49









            Giacomo CatenazziGiacomo Catenazzi

            2,063515




            2,063515







            • 1





              This did not answer the question.

              – Thomas Dickey
              Feb 14 '16 at 16:23






            • 1





              No. less -R actually resolved the issue for me.

              – Ken Ingram
              Jan 1 at 2:25












            • 1





              This did not answer the question.

              – Thomas Dickey
              Feb 14 '16 at 16:23






            • 1





              No. less -R actually resolved the issue for me.

              – Ken Ingram
              Jan 1 at 2:25







            1




            1





            This did not answer the question.

            – Thomas Dickey
            Feb 14 '16 at 16:23





            This did not answer the question.

            – Thomas Dickey
            Feb 14 '16 at 16:23




            1




            1





            No. less -R actually resolved the issue for me.

            – Ken Ingram
            Jan 1 at 2:25





            No. less -R actually resolved the issue for me.

            – Ken Ingram
            Jan 1 at 2:25

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • 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%2funix.stackexchange.com%2fquestions%2f262185%2fdisplay-file-with-ansi-colors%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

            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?