Showing posts with label echo. Show all posts
Showing posts with label echo. Show all posts

Thursday, December 18, 2008

Linux:shell:Bash: How to change color of your shell printout


You could use 'echo -e' to change your printout text and background color by some color variables. One of the easiest ways to use them is to define shell variable, for example, define those variables in your shell script:

red='\e[0;31m'
RED='\e[1;31m'

blue='\e[0;34m'

BLUE='\e[1;34m'

cyan='\e[0;36m'

CYAN='\e[1;36m'
NC='\e[0m' # No Color


. '\e[0;36m': 0's place is the color of background (0, no effect, 1, bold, 4, underline) and 36 is the text color. You also can change background colors due to color values in the color table. For example, '\e[41;34m' changes background color to red and text color to blue.

How to print?

echo -e "$red These texts are in red$NC" #you must use '-e' to enable interpretation of the backslash-escaped characters, see echo's help.

or if you want to use it in 'printf':

printf "%s\n" "`echo -e $red`These texts are in red`echo -e $NC`"

There is a table of color values attached (this table is from the link). You can find color values what you want.

Friday, July 20, 2007

Linux:shell:grep how to use grep's result

mystr="IloveChina"
echo $mystr |grep China
echo $? #this will return 0
echo $mystr |grep USA
echo $? #this will return 1
#so you could use this returned value to justify if grep found the string.
# Or you use how many searched results returned to do this, for example,
echo $mystr |grep -c China # return 1 to you
echo $mystr |grep -c USA # return 0 to you
# so simply, you can use the number to justify if you found the results like
if [ `
echo $mystr |grep -c China` -ge 1 ]; then
echo "Yes, you are"
fi




Locations of visitors to this page