echo command with practical examples Linux

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. echo command with practical examples Linux, the mostly used command in shell scripting /bash scripting / Unix shell scripting.

AUTHOR of echo is written by Brian Fox and Chet Ramey

1. Print single line of echo statement

Just we can print an single line of statement

$ echo Tech Tutorials is website to Learn new things

2. Exact statement ignore commands within the echo

Printing a echo with exact format lets see the example

$ 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. Variable value in echo statement

Lets set an variable value and print the same within the echo statement

In this example defining the variable value as HOMEDIR=/home/ravi/

$ HOMEDIR=/home/ravi/
$ echo "$HOMEDIR is home directory of ravi"

lets see the other example here

$ X=10
$ 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

$ echo Tech\ Tutorial
 Tech Tutorial
$ 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 by adding new line option in echo. 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 how it works with echo command

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

$ 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.

$ echo -E "Tech \tTutorial is a \vWebsite"
 Tech \tTutorial is a \vWebsite

15. echo command Hexadecimal value

$ 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

# /bin/echo --version
echo (GNU coreutils) 8.22

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 “–version”.

17. Create an file using echo command

using ‘>’ standard output redirection along with echo statement it will create an file

$ echo "Tech Tutorials is a website" > TechTutorial

18. echo prints one more extra character

Recently observed is that when you echo two character statement using echo command, when you count using wc is shows three characters example is below

$ echo 12 |wc -c
3

Why echo character count is more.?

because when you echo a statement by default echo adds new line character that’s why always echo characters are added by one more extra character. How to avoid adding new line character on echo? use -n option which prevents adding new line character in echo command.

$ echo -n 12 |wc -c
2

Along with ‘-e’ option we can use below options

  • \\ backslash
  • \a alert (BEL)
  • \b backspace
  • \c produce no further output
  • \e escape
  • \f form feed
  • \n new line
  • \r carriage return
  • \\ backslash
  • \a alert (BEL)
  • \b backspace
  • \c produce no further output
  • \e escape
  • \f form feed
  • \n new line
  • \r carriage return
  • \t horizontal tab
  • \v vertical tab
  • \0NNN byte with octal value NNN (1 to 3 digits)
  • \xHH byte with hexadecimal value HH (1 to 2 digits)
  • \t horizontal tab
  • \v vertical tab
  • \0NNN byte with octal value NNN (1 to 3 digits)
  • \xHH byte with hexadecimal value HH (1 to 2 digits)

Please provide your valuable comments …

Related Articles

Monitoring Real time CPU Usage and Get alert Script

Complete shell Scripting Training Online course Content

Generate Nagios configuration Files using shell Script

shell Script building blocks

Video Tutorial

Thanks for your wonderful Support and Encouragement