Linux Tips and Tricks

Linux Tips and Tricks

Exploring echo and Colorful echo

March13

echo command

echo  command is used to places the text in the terminal or console

1) Create a file using the echo command.

1
$ echo "The Linux Tips" > myfile.txt

2) Append the text in the existing file

1
2
3
4
5
6
7
8
$ cat myfile.txt
The Linux Tips
 
$ echo "The Linux Tips - Second Line" >> myfile.txt
 
$ cat myfile.txt
The Linux Tips
The Linux Tips - Second Line

3) echo and its arguments

-n -> It will not output the trailing newline

1
2
$ echo -n "The Linux Tips"
The Linux Tips$

Normal one ( trailing newline )

1
2
3
$ echo "The Linux Tips"
The Linux Tips
$

-e -> It will enable the interpretation of backslash escapes

1
2
3
4
#In the below example, i used \n (new line), but without using -e it prints the \n
 
$ echo "The Linux \n Tips "
The Linux \n Tips
1
2
3
$ echo -e "The Linux \n Tips"
The Linux
 Tips
1
2
3
4
5
6
#In the below example, i used \t (tab), but without using -e it prints the \t
$ echo "The Linux\tTips"
The Linux\tTips
 
$ echo -e "The Linux\tTips"
The Linux    Tips

With effective to -e option, we can use the below options

1
2
3
#print the \ (back slash)
 
$ echo -e "\\"

\

1
2
3
4
# \b is for backspace. In the below example, you can see the d character is removed
 
$ echo -e "abcd\bef"
abcef

 

1
2
3
4
# \c is used produce no further output. In the below example, ef is not printed
 
$ echo -e "abcd\cef"
abcd$

 

1
2
3
4
# \e is used for escape
 
$ echo -e "abcd\eef"
abcdef

 

1
2
3
4
5
# \f is for form feed.
 
$ echo -e "abcd\fef"
abcd
    ef

 

1
2
3
4
# \r is for carriage return
 
$ echo -e "abcd\ref"
efcd

 

1
2
3
4
# \t is for horizontal tab
 
$ echo -e "abcd\tef"
abcd    ef

 

1
2
3
4
5
# \v is for vertical tab
 
$ echo -e "abcd\vef"
abcd
    ef

-E -> disable interpretation of backslash escapes (default)

1
2
3
4
5
6
$ echo -E "The Linux \n Tips"
The Linux \n Tips
 
$ echo -e "The Linux \n Tips"
The Linux
 Tips

echo and its colors

The below program is using ANSI escape code SGR sequences. ( For more about the SGR sequence, search “Ansi escape code + wiki” in google )

Type the below program and save it as colors.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ cat colors.sh
#!/bin/sh
FGRED=`echo "\033[31m"`
FGCYAN=`echo "\033[36m"`
BGRED=`echo "\033[41m"`
FGBLUE=`echo "\033[35m"`
BGGREEN=`echo "\033[42m"`
 
NORMAL=`echo "\033[m"`
 
echo "${FGBLUE} Text in blue ${NORMAL}"
echo "Text normal"
echo "${BGRED} Background in red"
echo "${BGGREEN} Background in Green and back to Normal ${NORMAL}"

Assign the execute permission and execute the script.  code tag didn’t show the colors 🙁 in this page

1
2
3
4
5
$ ./colors.sh  
 Text in blue
Text normal
 Background in red
 Background in Green and back to Normal

After executing the script, you can see some colorful text and background colors

Change the numbers after the “[” ( opening square bracket ) and see the difference

For more information about colors, see here

Try with other numeric combinations and see different colors 🙂

 

bye
kamaraj

posted under Uncategorized

Email will not be published

Website example

Your Comment:


Recent Comments

    Categories