Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a ksh script that must work on both linux and solaris. I'm trying to color the output of specific commands. It works on linux (specifically RHEL6), but not on solaris (SunOS 5.10).

Command on linux (the output "test" is correctly colored red):

[amartin@linuxbox:~]$ echo "test" | sed 's,.*,\x1B[31m&\x1B[0m,'

Command on solaris (the output "test" is not colored):

[amartin@sunbox:~]$ echo "test" | sed 's,.*,\x1B[31m&\x1B[0m,'

Is there a way to craft this command such that the output is red, without the raw codes in the output? I can't change the 'echo' command because that's just a fill in for the command I'm actually running.

share|improve this question
up vote 10 down vote accepted

\xNN is an escape sequence in GNU sed, but it is not standard, and in particular it is not available on Solaris.

You can include a literal escape character in your script, but that would make it hard to read and edit.

You can use printf to generate an escape character. It understands octal escapes, not hexadecimal.

esc=$(printf '\033')
echo "test" | sed "s,.*,${esc}[31m&${esc}[0m,"

You can call tput to generate the replacement text in the call to sed. This command looks up escape sequences in the terminfo database. In theory, using tput makes your script more portable, but in practice you're unlikely to encounter a terminal that doesn't use ANSI escape codes.

echo "test" | sed "s,.*,$(tput setaf 1)&$(tput sgr0),"
share|improve this answer
this works great to colorize on a cat |sed too thx! – Aquarius Power Jun 14 '14 at 2:29

It would be easier to use tput

tput setaf 1; somecommand; tput sgr0


tput setaf 1
tput sgr0

This sets the foreground to red, runs somecommand which will then display the output in red then clears the color sequence. This works at least with bash, zsh and ksh.

See tmux(1) and terminfo(5) for more information about what you can do with tput.

share|improve this answer
thanks for the response. As I said earlier, I can't change the echo command. 'echo' is just a stand-in for the process I'm calling, which I cannot change. Is it possible to use sed with tput somehow? Also, when I run that command on sunOS 5.10 I don't see any colors. – acm Aug 20 '12 at 19:52
I updated it to break it apart into separate commands. – bahamat Aug 20 '12 at 19:55
This is better than my current implementation in that it does not print raw ascii codes on solaris. However (for whatever reason), it doesn't color the output of 'somecommand' on solaris. It does on linux. I have accepted @Gilles answer, which does both. Thank you. – acm Aug 21 '12 at 16:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.