Thursday, June 2, 2016

[Quick Tips: Echo Command]: echo command with 20 practical examples Linux

Echo Commands Examples:


echo command is used to print the line of text in a different ways, echo command is mostly used when writing an script, to print statement.
AUTHOR of echo is written by Brian Fox and Chet Ramey

1. Printing single line of echo statement

Just we can print an single line of statement
[root@TechTutorial ~]# echo Tech Tutorials is website to Learn new things
Tech Tutorials is website to Learn new things

2. Printing exact statement ignore commands within the echo

Printing a echo with exact format lets see the example
[root@TechTutorial ~]# echo '$0 $? Not working strings Tech Tutorials'
$0 $? Not working strings Tech Tutorials
if you observer an above example actually $0 and $? are the environment variable values but if you use ‘ ‘ statement within the single quotes it will print as exact

3. Printing an variable value

Lets set an variable value and print the same within the echo statement
In this example defining the variable value as HOMEDIR=/home/ravi/
[root@TechTutorial ~]# HOMEDIR=/home/ravi/
[root@TechTutorial ~]# echo "$HOMEDIR is home directory of ravi"
/home/ravi/ is home directory of ravi
lets see the other example here
 [root@TechTutorial ~]# X=10
[root@TechTutorial ~]# echo $X
10

4. Printing \ (back slash) in echo statement

if you run an echo statement with \ (back slash) it will not print in the echo statement because we call \ as escape character
Lets see the examples
[root@TechTutorial ~]# echo Tech\ Tutorial
Tech Tutorial
[root@TechTutorial ~]# echo -e "Tech\\ Tutorial\\"
Tech\ Tutorial\

5. Enabling interpretation of backslash escapes ‘-e’ option

In front of echo statement if we use ‘-e’ option then we can print echo statement in different formats
Printing same statement in multiple lines. To print echo statement in multiple lines we have to use ‘\n’ along with ‘-e’ option.
[root@TechTutorial ~]# echo -e "Tech Tutorials \nIs a Website \nLearn More"
Tech Tutorials
Is a Website
Learn More

6. Get the alert (bell) when it prints statement

Enable your speaker sound and run this, you will hear bell alert. We have to use ‘\a’ option along with ‘-e’ option
[root@TechTutorial ~]# echo -e "\aTech Tuturial is a website to learn new things"
Tech Tuturial is a website to learn new things

7. Run backspace using ‘\b’ option

backspace key lets see the example
as shown in below example echo statement is printed without space
[root@TechTutorial ~]# echo -e "Tech Tutorial is a \bwebsite \bto \blearn \bnew \bthings"
Tech Tutorial is awebsitetolearnnewthings

8. No Further output after ‘\c’ option along with ‘-e’ option

[root@TechTutorial ~]# echo -e "Tech Tutorial \cis a website"
Tech Tutorial [root@TechTutorial ~]#
if you see an above example after ‘\c’ option there is no output is printed

9. escape character using ‘\e’ option along with ‘-e’

[root@TechTutorial ~]# echo -e "Tech Tutorial \eWebsite"
Tech Tutorial ebsite
as shown in above example where we use option \e in front of option \e character has been deleted

10. Form feed and print in new line

[root@TechTutorial ~]# echo -e "Tech Tutorial \fWebsite"
Tech Tutorial
Website
whenever we use \f option it will clear the screen and print in multiple lines

11. Print in reverse format using echo command

[root@TechTutorial ~]# echo -e "Tech Tutorials \rwebsite"
websitetorials
above example we call it as carriage reverse

12. Horizontal tab in between echo statement

Using option ‘\t’ along with ‘-e’ will print the statement with tab space
[root@TechTutorial ~]# echo -e "Tech Tutorial \tis \ta \twebsite"
Tech Tutorial is a website

13. Vertical tab format

[root@TechTutorial ~]# echo -e "Tech \vTutorial \vis \va \vwebsite"
Tech
Tutorial
is
a
website
where we add ‘\v’ option along with ‘-e’ in echo statement will add vertical tab

14. Disabling interpretation of backslash escapes

As of now we used option ‘-e’ to enabling interpretation in order to disable the interpretation we have to use -E, Do remember difference between option -e and -E. If you want to print the echo statement with horizontal tab and vertical tab format do not use -E option.
[root@TechTutorial ~]# echo -E "Tech \tTutorial is a \vWebsite"
Tech \tTutorial is a \vWebsite

15. Print an Hexadecimal value using echo command

[root@TechTutorial ~]# echo -e '\x66\x66\x6f\x6f'
ffoo
As you see above example ‘\x66’ holds the ‘f’ hexadecimal character likewise we can print all the characters.

16. Know echo command version

[root@TechTutorial ~]# /bin/echo --version
echo (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Brian Fox and Chet Ramey.
if you run # echo –version then it will not print its version details because it assume that it is a echo statement and it will print “–verison”.

17. Create an file using echo command

using ‘>’ standard output redirection along with echo statement it will create an file
[root@TechTutorial ~]# echo "Tech Tutorials is a website" > TechTutorial
[root@TechTutorial ~]# cat TechTutorial
Tech Tutorials is a website
[root@TechTutorial ~]# ls -l TechTutorial
-rw-r--r--. 1 root root 28 Jan 17 16:06 TechTutorial
Along with ‘-e’ option we can use below options
\\backslash
\aalert (BEL)
\bbackspace
\cproduce no further output
\eescape
\fform feed
\nnew line
\rcarriage return
\thorizontal tab
\vvertical tab
\0NNNbyte with octal value NNN (1 to 3 digits)
\xHHbyte with hexadecimal value HH (1 to 2 digits)

No comments: