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?
If you are using zsh
or bash
black() {
echo -e "\e[30m${1}\e[0m"
}
red() {
echo -e "\e[31m${1}\e[0m"
}
green() {
echo -e "\e[32m${1}\e[0m"
}
yellow() {
echo -e "\e[33m${1}\e[0m"
}
blue() {
echo -e "\e[34m${1}\e[0m"
}
magenta() {
echo -e "\e[35m${1}\e[0m"
}
cyan() {
echo -e "\e[36m${1}\e[0m"
}
gray() {
echo -e "\e[90m${1}\e[0m"
}
black 'BLACK'
red 'RED'
green 'GREEN'
yellow 'YELLOW'
blue 'BLUE'
magenta 'MAGENTA'
cyan 'CYAN'
gray 'GRAY'
Try online
I've written swag to achieve just that.
You can just do
pip install swag
Now you can install all the escape commands as txt files to a given destination via:
swag install -d <colorsdir>
Or even easier via:
swag install
Which will install the colors to ~/.colors
.
Either you use them like this:
echo $(cat ~/.colors/blue.txt) This will be blue
Or this way, which I find actually more interesting:
swag print -c red -t underline "I will turn red and be underlined"
Check it out on asciinema!
to show the message output with diffrent color you can make :
echo -e "\033[31;1mYour Message\033[0m"
-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
echo -e "\033[31m Hello World"
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.
red='\e[0;31m'
NC='\e[0m' # No Color
echo -e "${red}Hello Stackoverflow${NC}"
This answer correct, except that the call to colors should not be inside the quotes.
echo -e ${red}"Hello Stackoverflow"${NC}
Should do the trick.
You can use the awesome tput
command (suggested in Ignacio's answer) to produce terminal control codes for all kinds of things.
Specific tput
sub-commands are discussed later.
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.
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.
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)"
The above command produces this on Ubuntu:
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.
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
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
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
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.
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 7\nsetb 1" | tput -S # set fg white and bg red
tput
command is listed in the Cap-name
column of the huge table that starts at line 81.)