Skip to main content

Bash Script: apply color in text


We can use the following colors' codes:
Black        0;30     Dark Gray     1;30
Blue         0;34     Light Blue    1;34
Green        0;32     Light Green   1;32
Cyan         0;36     Light Cyan    1;36
Red          0;31     Light Red     1;31
Purple       0;35     Light Purple  1;35
Brown/Orange 0;33     Yellow        1;33
Light Gray   0;37     White         1;37

And then use them like this in your script:
red='\e[0;31m'
NC='\e[0m' # No Color
echo -e "${red}Hello Stackoverflow${NC}"

Keep in mind you have to use '-e' after 'echo' and use double quote(") for text. 

Set

CodeDescriptionExamplePreview
1Bold/Bright
echo -e "Normal \e[1mBold"
Normal Bold
2Dim
echo -e "Normal \e[2mDim"
Normal Dim
4Underlined
echo -e "Normal \e[4mUnderlined"
Normal Underlined
5Blink 1)
echo -e "Normal \e[5mBlink"
Normal Blink
7Reverse (invert the foreground and background colors)
echo -e "Normal \e[7minverted"
Normal inverted
8Hidden (usefull for passwords)
echo -e "Normal \e[8mHidden"
Normal Hidden
Reset
CodeDescriptionExamplePreview
0Reset all attributes
echo -e "\e[0mNormal Text"
Normal Text
21Reset bold/bright
echo -e "Normal \e[1mBold \e[21mNormal"
Normal Bold Normal
22Reset dim
echo -e "Normal \e[2mDim \e[22mNormal"
Normal Dim Normal
24Reset underlined
echo -e "Normal \e[4mUnderlined \e[24mNormal"
Normal Underlined Normal
25Reset blink
echo -e "Normal \e[5mBlink \e[25mNormal"
Normal Blink Normal
27Reset reverse
echo -e "Normal \e[7minverted \e[27mNormal"
Normal inverted Normal
28Reset hidden
echo -e "Normal \e[8mHidden \e[28mNormal"
Normal Hidden Normal

Foreground (text)

CodeColorExamplePreview
39Default foreground color
echo -e "Default \e[39mDefault"
Default Default
30Black
echo -e "Default \e[30mBlack"
Default Black
31Red
echo -e "Default \e[31mRed"
Default Red
32Green
echo -e "Default \e[32mGreen"
Default Green
33Yellow
echo -e "Default \e[33mYellow"
Default Yellow
34Blue
echo -e "Default \e[34mBlue"
Default Blue
35Magenta
echo -e "Default \e[35mMagenta"
Default Magenta
36Cyan
echo -e "Default \e[36mCyan"
Default Cyan
37Light gray
echo -e "Default \e[37mLight gray"
Default Light gray
90Dark gray
echo -e "Default \e[90mDark gray"
Default Dark gray
91Light red
echo -e "Default \e[91mLight red"
Default Light red
92Light green
echo -e "Default \e[92mLight green"
Default Light green
93Light yellow
echo -e "Default \e[93mLight yellow"
Default Light yellow
94Light blue
echo -e "Default \e[94mLight blue"
Default Light blue
95Light magenta
echo -e "Default \e[95mLight magenta"
Default Light magenta
96Light cyan
echo -e "Default \e[96mLight cyan"
Default Light cyan
97White
echo -e "Default \e[97mWhite"
Default White

Background

CodeColorExamplePreview
49Default background color
echo -e "Default \e[49mDefault"
Default Default
40Black
echo -e "Default \e[40mBlack"
Default Black
41Red
echo -e "Default \e[41mRed"
Default Red
42Green
echo -e "Default \e[42mGreen"
Default Green
43Yellow
echo -e "Default \e[43mYellow"
Default Yellow
44Blue
echo -e "Default \e[44mBlue"
Default Blue
45Magenta
echo -e "Default \e[45mMagenta"
Default Magenta
46Cyan
echo -e "Default \e[46mCyan"
Default Cyan
47Light gray
echo -e "Default \e[47mLight gray"
Default Light gray
100Dark gray
echo -e "Default \e[100mDark gray"
Default Dark gray
101Light red
echo -e "Default \e[101mLight red"
Default Light red
102Light green
echo -e "Default \e[102mLight green"
Default Light green
103Light yellow
echo -e "Default \e[103mLight yellow"
Default Light yellow
104Light blue
echo -e "Default \e[104mLight blue"
Default Light blue
105Light magenta
echo -e "Default \e[105mLight magenta"
Default Light magenta
106Light cyan
echo -e "Default \e[106mLight cyan"
Default Light cyan
107White
echo -e "Default \e[107mWhite"
Default White

Comments

Popular posts from this blog

Nginx startup script

#!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig:   - 85 15 # description:  NGINX is an HTTP(S) server, HTTP(S) reverse \ #               proxy and IMAP/POP3 proxy server # processname: nginx # config:      /etc/nginx/nginx.conf # config:      /etc/sysconfig/nginx # pidfile:     /var/run/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/local/nginx/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx make_dirs() {    # make required directories    user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed...

Tomcat NIO Connector and How to Configure It

In  HTTP 1.1 , all connections between the browser and the server are considered persistent unless declared otherwise. Persistence, in this context, means to use a single TCP connection to send and receive multiple HTTP requests/responses, as opposed to opening a new connection for every single request/response pair. In tomcat, the default HTTP connector is blocking and follows a one thread per connection model. This means that in order to serve 100 concurrent users, it requires 100 active threads. We end up wasting resources (the thread) because connections may not be used heavily, but just enough to avoid a timeout. Opposed to this is the relatively new NIO or non blocking connector. This connector has a couple of poller threads used to keep the connection alive for all connected users while worker threads are called whenever data (a new HTTP request) is available. This model leads to a much better sharing of resources (threads) and a larger number of concurrent users can ...

Java + HTTPS: Unable to Find Valid Certification Path to Requested Target

Run  InstallCert.java , with your hostname and https port, and press “ 1 ” when ask for input. It will add your “localhost” as a trusted keystore, and generate a file named “ jssecacerts “. C : \ > java InstallCert localhost : 8443 Loading KeyStore C : \Program Files\Java\jre6\lib\security\cacerts . . . Opening connection to localhost : 8443 . . . Starting SSL handshake . . . javax . net . ssl . SSLHandshakeException : sun . security . validator . ValidatorException : PKIX path building failed : sun . security . provider . certpath . SunCertPathBuilderException : unable to find valid certification path to requested target at com . sun . net . ssl . internal . ssl . Alerts . getSSLException ( Unknown Source ) at com . sun . net . ssl . internal . ssl . SSLSocketImpl . fatal ( Unknown Source ) at com . sun . net . ssl . internal . ssl . Handshaker . fatalSE ( Unknown Source ) at com . sun . net . ssl . internal . ssl . Handshaker...