Bash Colors

You can make your BASH script more pretty, by colorizing its output.

Use ANSI escape sequences to set text properties like foreground and background colors.

Colorizing Shell

Use the following template for writing colored text:

echo -e "\e[COLORmSample Text\e[0m"
Option Description
-e Enable interpretation of backslash escapes
\e[ Begin the color modifications
COLORm Color Code + ‘m’ at the end
\e[0m End the color modifications

Examples:

$ echo -e "\e[31mRed Text\e[0m"
Red Text
$ echo -e "\e[42mGreen Background\e[0m"
Green Background

ANSI — Color Escape Codes

Shell scripts commonly use ANSI escape codes for color output:

Color Foreground Code Background Code Sample
Black 30 40
Red 31 41
Green 32 42
Brown 33 43
Blue 34 44
Purple 35 45
Cyan 36 46
Light Gray 37 47

Escape sequence also allows to control the manner in which characters are displayed on the screen:

ANSI Code Description
0 Normal Characters
1 Bold Characters
4 Underlined Characters
5 Blinking Characters
7 Reverse video Characters

Examples:

$ echo -e "\e[1mBold Text\e[0m"
Bold Text
$ echo -e "\e[3mUnderlined Text\e[0m"
Underlined Text

By combining all these escape sequences, we can get more fancy effect.

echo -e "\e[COLOR1;COLOR2mSample Text\e[0m"

There are some differences between colors when combining colors with bold text attribute:

Color Foreground Code Background Code Sample
Dark Gray 1;30 1;40
Light Red 1;31 1;41
Light Green 1;32 1;42
Yellow 1;33 1;43
Light Blue 1;34 1;44
Light Purple 1;35 1;45
Light Cyan 1;36 1;46
White 1;37 1;47

Examples:

$ echo -e "\e[1;34mLight Blue Text\e[0m"
Light Blue Text
$ echo -e "\e[1;33;4;44mYellow Underlined Text on Blue Background\e[0m"
Yellow Underlined Text on Blue Background

HowTo: Check If a String Exists

Sometimes, we need to check if the pattern presents in a file and take some actions depending on the result.

It can be done with the help of ‘exit status codes’. Each Linux command returns a status when it terminates normally or abnormally

You can use command exit status in the shell script to display an error message or perform some sort of action.

Syntax

grep -q [PATTERN] [FILE] && echo $?
  • The exit status is 0 (true) if the pattern was found;
  • The exit status is 1 (false) if the pattern was not found.

Examples

Here are some examples of checking if the string, that contains some pattern, presents it the file.

Example 1:

The following example shows that ‘SOME_PATTERN’ presents in ‘SOME_FILE’.

grep -q 'SOME_PATTERN' 'SOME_FILE' && echo $?
0

Example 2:

The next example shows that ‘ANOTHER_ONE_PATTERN’ doesn’t present in ‘SOME_FILE’.

grep -q 'ANOTHER_ONE_PATTERN' 'SOME_FILE' && echo $?
1

Example 3:

Checking if the string presents, and printing the error message if it doesn’t.

grep -q 'PATTERN' 'FILE' || echo "Error: The Pattern Does Not Exist";

Output:

Error: The Pattern Does Not Exist

BASH Script

Let’s check if the pattern exist in the file. If it exists, we will print all the strings that contain the pattern. If it doesn’t exist we will print the error message and stop the script.

#!/bin/bash
PATTERN=$1
FILE=$2
if grep -q $PATTERN $FILE;
 then
     echo "Here are the Strings with the Pattern '$PATTERN':"
     echo -e "$(grep $PATTERN $FILE)\n"
 else
     echo "Error: The Pattern '$PATTERN' was NOT Found in '$FILE'"
     echo "Exiting..."
     exit 0
fi

Save the script, give the execute permissions and execute:

chmod +x script.sh
./script.sh root /etc/passwd
Here are the Strings with the Pattern 'root':
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
./script.sh John /etc/passwd
Error: The Pattern 'John' was NOT Found in '/etc/passwd'
Exiting...

Bash: Test If File Exists

While creating a bash script, it is commonly helpful to test if file exists before attempting to perform some action with it.

This is a job for the test command, that allows to check if file exists and what type is it.

As only the check is done – the test command sets the exit code to 0 (TRUE) or 1 (FALSE), whenever the test succeeded or not.

Also the test command has a logical “not” operator which allows to get the TRUE answer when it needs to test if file does not exist. (more…)

Bash: Read File Line By Line – While Read Line Loop

The while loop is the best way to read a file line by line in Linux.

If you need to read a file line by line and perform some action with each line – then you should use a while read line construction in Bash, as this is the most proper way to do the necessary.

In this article i will show the general syntax of the while read line construction in Bash and an example of how to read a file line by line from the Linux command line.

I will also show an example of the Bash script that reads an input file line by line and prints each line with some appended text. (more…)