15 Practical Examples of ‘echo’ command in Linux

echo is one of the most commonly and widely used built-in command for Linux bash and C shells, that typically used in scripting language and batch files to display a line of text/string on standard output or a file.

echo command

echo command examples

The syntax for echo is:

echo [option(s)] [string(s)]

1. Input a line of text and display on standard output

$ echo Tecmint is a community of Linux Nerds 

Outputs the following text:

Tecmint is a community of Linux Nerds 

2. Declare a variable and echo its value. For example, Declare a variable of x and assign its value=10.

$ x=10

echo its value:

$ echo The value of variable x = $x 

The value of variable x = 10 

Note: The ‘-e‘ option in Linux acts as interpretation of escaped characters that are backslashed.

3. Using option ‘\b‘ – backspace with backslash interpretor ‘-e‘ which removes all the spaces in between.

$ echo -e "Tecmint \bis \ba \bcommunity \bof \bLinux \bNerds" 

TecmintisacommunityofLinuxNerds 

4. Using option ‘\n‘ – New line with backspace interpretor ‘-e‘ treats new line from where it is used.

$ echo -e "Tecmint \nis \na \ncommunity \nof \nLinux \nNerds" 

Tecmint 
is 
a 
community 
of 
Linux 
Nerds 

5. Using option ‘\t‘ – horizontal tab with backspace interpretor ‘-e‘ to have horizontal tab spaces.

$ echo -e "Tecmint \tis \ta \tcommunity \tof \tLinux \tNerds" 

Tecmint 	is 	a 	community 	of 	Linux 	Nerds 

6. How about using option new Line ‘\n‘ and horizontal tab ‘\t‘ simultaneously.

$ echo -e "\n\tTecmint \n\tis \n\ta \n\tcommunity \n\tof \n\tLinux \n\tNerds" 

	Tecmint 
	is 
	a 
	community 
	of 
	Linux 
	Nerds 

7. Using option ‘\v‘ – vertical tab with backspace interpretor ‘-e‘ to have vertical tab spaces.

$ echo -e "\vTecmint \vis \va \vcommunity \vof \vLinux \vNerds" 

Tecmint 
        is 
           a 
             community 
                       of 
                          Linux 
                                Nerds 

8. How about using option new Line ‘\n‘ and vertical tab ‘\v‘ simultaneously.

$ echo -e "\n\vTecmint \n\vis \n\va \n\vcommunity \n\vof \n\vLinux \n\vNerds" 


Tecmint 

is 

a 

community 

of 

Linux 

Nerds 

Note: We can double the vertical tab, horizontal tab and new line spacing using the option two times or as many times as required.

9. Using option ‘\r‘ – carriage return with backspace interpretor ‘-e‘ to have specified carriage return in output.

$ echo -e "Tecmint \ris a community of Linux Nerds" 

is a community of Linux Nerds 

10. Using option ‘\c‘ – suppress trailing new line with backspace interpretor ‘-e‘ to continue without emitting new line.

$ echo -e "Tecmint is a community \cof Linux Nerds" 

Tecmint is a community [email protected]:~$ 

11. Omit echoing trailing new line using option ‘-n‘.

$ echo -n "Tecmint is a community of Linux Nerds" 
Tecmint is a community of Linux [email protected]:~/Documents$ 

12. Using option ‘\a‘ – alert return with backspace interpretor ‘-e‘ to have sound alert.

$ echo -e "Tecmint is a community of \aLinux Nerds" 
Tecmint is a community of Linux Nerds

Note: Make sure to check Volume key, before firing.

13. Print all the files/folder using echo command (ls command alternative).

$ echo * 

103.odt 103.pdf 104.odt 104.pdf 105.odt 105.pdf 106.odt 106.pdf 107.odt 107.pdf 108a.odt 108.odt 108.pdf 109.odt 109.pdf 110b.odt 110.odt 110.pdf 111.odt 111.pdf 112.odt 112.pdf 113.odt linux-headers-3.16.0-customkernel_1_amd64.deb linux-image-3.16.0-customkernel_1_amd64.deb network.jpeg 

14. Print files of a specific kind. For example, let’s assume you want to print all ‘.jpeg‘ files, use the following command.

$ echo *.jpeg 

network.jpeg 

15. The echo can be used with redirect operator to output to a file and not standard output.

$ echo "Test Page" > testpage 

## Check Content
[email protected]:~$ cat testpage 
Test Page 
echo Options
 Options  Description
 -n  do not print the trailing newline.
 -e  enable interpretation of backslash escapes.
 \b  backspace
 \\  backslash
 \n  new line
 \r  carriage return
 \t  horizontal tab
 \v  vertical tab

That’s all for now and don’t forget to provide us with your valuable feedback in the comments below.

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

29 Responses

  1. Carl says:

    Hello , if i want to send something to any place, ( # echo ‘xxxxx’ >> /etc/apt/xxxx ) what should be the difference of use:
    echo ‘x’ and echo “x”, ie : echo with single mark and echo with double quotation marks
    thanks.

  2. Haz says:

    If I want to print something on stderr, how do I do it, without using /dev/stderr

  3. Yoander says:

    If you want to avoid variable expansion close string in simple stick

    echo ‘Hello $x’

    print

    Hello $x

  4. Stef says:

    A very convenient escape is \e. It is used to start a terminal escape sequence.
    For example, the following should print Hello in red and World in green:
    echo -e ‘\e[31mHello\e[32mWorld\e[0m’

    There is also \xHH to echo the character corresponding to an hexadecimal value.

    In Bash and probably in other shells, echo is a builtin with some extensions.
    The escape \uHHHH or \UHHHHHHHHHH can be use to echo unicode characters.
    For example, \u20AC should echo be the Euro sign €

    • Francesco Lazzarotto says:

      Good, I checked but it works with double quotes, not with single quotes or without
      echo -e “\e[31mHello\e[32mWorld\e[0m”
      echo -e “\u20AC”
      tested with my
      GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu)
      Ubuntu 12.04.5 LTSLinux 3.2.0-89-generic x86_64

  5. ranzk158 says:

    Hey… great job man.

    There is one more important feature of echo. You can use ‘echo’ in replacement for ‘cat’ to create text files.

    Eg: $echo “This is a test file” > test1
    This creates a text file named “test1” with text(This is a test file) in it.

  6. Eric Parent says:

    Great article, thanks! One minor correction, from #4 and on, you refer the -e option as the “backspace interpreter” (as opposed to the “backslash interpreter”). I think it’s possibly because #3 got you a bit mixed up when you discuss both the “backspace” (\b) and the “backslash interpreter” (-e).

    Anyway, not really important, just thought I’d point it out in case you ever make revisions to this article.

  7. Naresh says:

    The o\p of echo and ls is the same .?

    • Avishek Kumar says:

      Not Always.

      Though echo * can be used in place of ls.
      i don’t find a way to use ls in place of echo.

      ls works with name of the file where as echo with the content of the file, in most general use.

      Hope it Helps. Keep connected

  8. Himanshu says:

    echo ” Helpful post, thanks for sharing.” :D

  9. champ007 says:

    So helpful bhai keep going.

  10. anand says:

    $ echo “*.pyc” >> .gitignore

    to append something to a file without opening it.
    It use it almost everyday while working with git.

    • Marrus says:

      More practical than most of the above, at least for me.

      • Marrus says:

        Still, very useful. I did know that echo could be somewhat useful, but this is… wow. Many thanks to you, Avishek, you deserve them.
        (I read this, came back a couple o’days later read the comments and answer without thinking. Now I sound like a prick, while I just wanted to point out that the ability to append sth. to a file is the case I, personally, used echo the most for)

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.