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?
For readability
If you want to improve the readability of the code, you can
echo
the string first then add the color later by usingsed
:Thanks to @k-five for this answer
Result
Hope this image help you to pick your color for your bash :D
You can use the awesome
tput
command (suggested in Ignacio's answer) to produce terminal control codes for all kinds of things.Usage
Specific
tput
sub-commands are discussed later.Direct
Call
tput
as part of a sequence of commands:Use
;
instead of&&
so iftput
errors the text still shows.Shell variables
Another option is to use shell variables:
tput
produces character sequences that are interpreted by the terminal as having a special meaning. They will not be shown themselves. Note that they can still be saved into files or processed as input by programs other than the terminal.Command substitution
It may be more convenient to insert
tput
's output directly into yourecho
strings using command substitution:Example
The above command produces this on Ubuntu:
Foreground & background colour commands
Colours are as follows:
There are also non-ANSI versions of the colour setting functions (
setb
instead ofsetab
, andsetf
instead ofsetaf
) which use different numbers, not given here.Text mode commands
Cursor movement commands
Clear and insert commands
Other commands
With compiz wobbly windows, the
bel
command makes the terminal wobble for a second to draw the user's attention.Scripts
tput
accepts scripts containing one command per line, which are executed in order beforetput
exits.Avoid temporary files by echoing a multiline string and piping it:
See also
man 1 tput
man 5 terminfo
for the complete list of commands and more details on these options. (The correspondingtput
command is listed in theCap-name
column of the huge table that starts at line 81.)The
[31m
controls the text color:30
-37
sets foreground color40
-47
sets background colorA more complete list of color codes can be found here.
It is good practice to reset the text color back to
\033[0m
at the end of the string.You can use these ANSI escape codes:
And then use them like this in your script:
which prints
love
in red.From @james-lim's comment, if you are using the
echo
command, be sure to use the -e flag to allow backslash escapes.(don't add
"\n"
when using echo unless you want to add additional empty line)My favourite answer so far is coloredEcho.
Just to post another option, you can check out this little tool xcol
https://ownyourbits.com/2017/01/23/colorize-your-stdout-with-xcol/
you use it just like grep, and it will colorize its stdin with a different color for each argument, for instance
Note that it accepts any regular expression that sed will accept.
This tool uses the following definitions
I use these variables in my scripts like so