I am trying to print a text in the terminal using echo command.
I want to print the text in a red color. How can I do that?
I am trying to print a text in the terminal using echo command.
I want to print the text in a red color. How can I do that?
To expand on this answer, for the lazy of us:
Here is a simple little script, I put together recently, that will colorize any piped input instead of using "Toilet".
File: color.bsh
Then call it with color red (196):
$> echo "text you want colored red" | color.bsh 196
Just as something a little out there, passing it through grep will highlight it as red (but only red). You can also use named pipes so your string is nearer to the end of the line:
Use
tput
with thesetaf
capability and a parameter of1
.Use
tput
to calculate color codes. Avoid using the ANSI escape code (e.g.\E[31;1m
for red) because it's less portable. Bash on OS X, for example, does not support it.This question has been answered over and over again :-) but why not.
First using
tput
is more portable in modern environments than manually injecting ASCII codes throughecho -E
Here's a quick bash function:
Now you can use:
to get:
Notes on portability of
tput
First time
tput(1)
source code was uploaded in September 1986tput(1)
has been available in X/Open curses semantics in 1990s (1997 standard has the semantics mentioned below).So, it's (quite) ubiquitous.