I have a file with ANSI colors.


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.

  • 7
    Using less -R, for instance. Feb 14, 2016 at 11:43
  • What does your cat print?
    – techraf
    Feb 14, 2016 at 11:57
  • 1
    OP's question had too many possible answers to be interesting. Start with echo -e $(cat test.txt). Feb 14, 2016 at 16:31

4 Answers 4


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


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         mple\e[0m
\e[0;31mExample line2\e[0m
  • 1
    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, 2018 at 3:04
  • 4
    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, 2018 at 21:40

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.


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


 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.


There is a package that contains an executable that is specifically does this:


You can install it on Debian derived Linux installations with either

sudo apt install ttyrec

or on RedHat ones with

sudo yum install ttyrec

The command to replay a file properly is executed with

ttyplay <filename>

Also checkout a package named tty2gif, which converts similar files to gif.


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

  • 2
    This did not answer the question. Feb 14, 2016 at 16:23
  • 1
    No. less -R actually resolved the issue for me.
    – Ken Ingram
    Jan 1, 2019 at 2:25
  • Giacomo Catenazzi is right. If the file contains valid escape sequences, the cat will display them. Since cat does not interpret sequences like echo -e does, the file should really contain single escape character HEX 1B at the beginning of a colour sequence, not \033 or \x1B or similar.
    – anilech
    Nov 29, 2023 at 13:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .