How to change the output color of echo in Linux
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?
You can use these ANSI escape codes:
Black 0;30 Dark Gray 1;30
Red 0;31 Light Red 1;31
Green 0;32 Light Green 1;32
Brown/Orange 0;33 Yellow 1;33
Blue 0;34 Light Blue 1;34
Purple 0;35 Light Purple 1;35
Cyan 0;36 Light Cyan 1;36
Light Gray 0;37 White 1;37
And then use them like this in your script:
# .---------- constant part!
# vvvv vvvv-- the code from above
RED=' 33[0;31m'
NC=' 33[0m' # No Color
printf "I ${RED}love${NC} Stack Overflown"
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.
# Continued from above example
echo -e "I ${RED}love${NC} Stack Overflow"
(don't add "n"
when using echo unless you want to add additional empty line)
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:
tput setaf 1; echo "this is red text"
Use ;
instead of &&
so if tput
errors the text still shows.
Shell variables
Another option is to use shell variables:
red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0`
echo "${red}red text ${green}green text${reset}"
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 your echo
strings using command substitution:
echo "$(tput setaf 1)Red text $(tput setab 7)and white background$(tput sgr 0)"
Example
The above command produces this on Ubuntu:
Foreground & background colour commands
tput setab [1-7] # Set the background colour using ANSI escape
tput setaf [1-7] # Set the foreground colour using ANSI escape
Colours are as follows:
Num Colour #define R G B
0 black COLOR_BLACK 0,0,0
1 red COLOR_RED 1,0,0
2 green COLOR_GREEN 0,1,0
3 yellow COLOR_YELLOW 1,1,0
4 blue COLOR_BLUE 0,0,1
5 magenta COLOR_MAGENTA 1,0,1
6 cyan COLOR_CYAN 0,1,1
7 white COLOR_WHITE 1,1,1
There are also non-ANSI versions of the colour setting functions ( setb
instead of setab
, and setf
instead of setaf
) which use different numbers, not given here.
Text mode commands
tput bold # Select bold mode
tput dim # Select dim (half-bright) mode
tput smul # Enable underline mode
tput rmul # Disable underline mode
tput rev # Turn on reverse video mode
tput smso # Enter standout (bold) mode
tput rmso # Exit standout mode
Cursor movement commands
tput cup Y X # Move cursor to screen postion X,Y (top left is 0,0)
tput cuf N # Move N characters forward (right)
tput cub N # Move N characters back (left)
tput cuu N # Move N lines up
tput ll # Move to last line, first column (if no cup)
tput sc # Save the cursor position
tput rc # Restore the cursor position
tput lines # Output the number of lines of the terminal
tput cols # Output the number of columns of the terminal
Clear and insert commands
tput ech N # Erase N characters
tput clear # Clear screen and move the cursor to 0,0
tput el 1 # Clear to beginning of line
tput el # Clear to end of line
tput ed # Clear to end of screen
tput ich N # Insert N characters (moves rest of line forward!)
tput il N # Insert N lines
Other commands
tput sgr0 # Reset text format to the terminal's default
tput bel # Play a bell
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 before tput
exits.
Avoid temporary files by echoing a multiline string and piping it:
echo -e "setf 7nsetb 1" | tput -S # set fg white and bg red
See also
man 1 tput
man 5 terminfo
for the complete list of commands and more details on these options. (The corresponding tput
command is listed in the Cap-name
column of the huge table that starts at line 81.) some variables that you can use:
# Reset
Color_Off=' 33[0m' # Text Reset
# Regular Colors
Black=' 33[0;30m' # Black
Red=' 33[0;31m' # Red
Green=' 33[0;32m' # Green
Yellow=' 33[0;33m' # Yellow
Blue=' 33[0;34m' # Blue
Purple=' 33[0;35m' # Purple
Cyan=' 33[0;36m' # Cyan
White=' 33[0;37m' # White
# Bold
BBlack=' 33[1;30m' # Black
BRed=' 33[1;31m' # Red
BGreen=' 33[1;32m' # Green
BYellow=' 33[1;33m' # Yellow
BBlue=' 33[1;34m' # Blue
BPurple=' 33[1;35m' # Purple
BCyan=' 33[1;36m' # Cyan
BWhite=' 33[1;37m' # White
# Underline
UBlack=' 33[4;30m' # Black
URed=' 33[4;31m' # Red
UGreen=' 33[4;32m' # Green
UYellow=' 33[4;33m' # Yellow
UBlue=' 33[4;34m' # Blue
UPurple=' 33[4;35m' # Purple
UCyan=' 33[4;36m' # Cyan
UWhite=' 33[4;37m' # White
# Background
On_Black=' 33[40m' # Black
On_Red=' 33[41m' # Red
On_Green=' 33[42m' # Green
On_Yellow=' 33[43m' # Yellow
On_Blue=' 33[44m' # Blue
On_Purple=' 33[45m' # Purple
On_Cyan=' 33[46m' # Cyan
On_White=' 33[47m' # White
# High Intensity
IBlack=' 33[0;90m' # Black
IRed=' 33[0;91m' # Red
IGreen=' 33[0;92m' # Green
IYellow=' 33[0;93m' # Yellow
IBlue=' 33[0;94m' # Blue
IPurple=' 33[0;95m' # Purple
ICyan=' 33[0;96m' # Cyan
IWhite=' 33[0;97m' # White
# Bold High Intensity
BIBlack=' 33[1;90m' # Black
BIRed=' 33[1;91m' # Red
BIGreen=' 33[1;92m' # Green
BIYellow=' 33[1;93m' # Yellow
BIBlue=' 33[1;94m' # Blue
BIPurple=' 33[1;95m' # Purple
BICyan=' 33[1;96m' # Cyan
BIWhite=' 33[1;97m' # White
# High Intensity backgrounds
On_IBlack=' 33[0;100m' # Black
On_IRed=' 33[0;101m' # Red
On_IGreen=' 33[0;102m' # Green
On_IYellow=' 33[0;103m' # Yellow
On_IBlue=' 33[0;104m' # Blue
On_IPurple=' 33[0;105m' # Purple
On_ICyan=' 33[0;106m' # Cyan
On_IWhite=' 33[0;107m' # White
the escape character in bash , hex and octal respectively:
| | bash | hex | octal | NOTE |
|-------+-------+--------+---------+------------------------------|
| start | e | x1b | 33 | |
| start | E | x1B | - | x cannot be capital |
| end | e[0m | x1m0m | 33[0m | |
| end | e[m | x1b[m | 33[m | 0 is appended if you omit it |
| | | | | |
short example:
| color | bash | hex | octal | NOTE |
|-------------+--------------+----------------+----------------+---------------------------------------|
| start green | e[32m<text> | x1b[32m<text> | 33[32m<text> | m is NOT optional |
| reset | <text>e[0m | <text>1xb[0m | <text>