Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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,'
test

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

[amartin@sunbox:~]$ echo "test" | sed 's,.*,\x1B[31m&\x1B[0m,'
x1B[31mtestx1B[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
add comment

2 Answers 2

up vote 5 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 at 2:29
add comment

It would be easier to use tput

tput setaf 1; somecommand; tput sgr0

or

tput setaf 1
somecommand
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
add comment

Your Answer

 
discard

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.