15 Practical Examples of ‘echo’ command in Linux

The echo command is one of the most commonly and widely used built-in commands for Linux bash and C shells, that typically used in a 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 the echo command is:

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

1. Input a line of text and display it 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 an 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 avi@tecmint:~$ 

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

$ echo -n "Tecmint is a community of Linux Nerds" 
Tecmint is a community of Linux Nerdsavi@tecmint:~/Documents$ 

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

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

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

13. Print all the files/folders 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 a redirect operator to output to a file and not standard output.

$ echo "Test Page" > testpage 

## Check Content
avi@tecmint:~$ 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.

If you read this far, tweet to the author to show them you care. Tweet a thanks
Ronav Saive
A Passionate GNU/Linux Enthusiast and Software Developer with over a decade in the field of Linux and Open Source technologies.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

32 thoughts on “15 Practical Examples of ‘echo’ command in Linux”

  1. 2020 and I am the only one to notice the repeated error over and over again?

    Instead of “backSPACE interpreter” you mean “backSLASH interpreter.”

    You are welcome.

    Reply
  2. Hi All,

    I am stuck in my task and not able to get through. I would really appreciate your help.

    So, here is the thing.. I am trying to pass a file to mapper (where the required output location is mentioned) using the command.

    # file2broker -f TestfilePlm.txt -q SIABROKER1.INBOUND -m MERT -z appid=PLM0002F 
    

    trying to pass TestfilePlm.txt file via inbound queue and the mapper should execute the lines for the PLM0002F app id.

    Problem: The output file is not getting generated nor the data or TestfilePlm.txt is in error queue.

    I tried echo $? after file2broker command,it is returning 11 code.
    Can someone please tell me what 11 here signifies?

    I am not sure what I am missing here.

    Reply
  3. I use alias ?=’echo ‘ in my .bashrc

    So, to echo an calculation:
    ? 2.2*128.56| bc -iq | tail -n 1

    And to echo:
    ? Hello World! Whats up?

    Reply
  4. how to display hello in block letters ,to display in red color and blinking effect using echo command in shell scripting
    Plz explain

    Reply
  5. Great list of echo usage. I was going through a particular example where the need is to reformat linux date into human readable date.

    Like: echo $(date +%Y-%m%d\ %H:%M:%S).

    My question is what are the all types of formatting one can do using echo. for example : Can i make particular word or letter in bold? or any other commonly used formatting.

    Reply
  6. I am trying the ‘for do’ loop below and experimenting with the echo options. When I submit the script as follows:

    ./forDoLoop.sh peter pan flies

    the text ‘peter pan flies’ doesn’t stay on the screen, it briefly displays the text but then disappears. Please can you tell me what I am doing wrong in the loop below.

    for do loop
    ————–
    for TOKEN in $*
    do
    echo -n $TOKEN
    done

    Reply
  7. Hi All ,
    I am beginner to Linux Environment. My problem is, i want to append same text to a file multiple times in single command. How to do it.which command i have to use:
    For Single append i am using

    echo “Hello world” >> testfile.txt (for 1 time)
    cat testfile.txt

    if i want to append 10 times that same Hello world , need to write
    echo “Hello world” >> testfile.txt 10 times? or any other possibilities please.

    i want out put like below:

    Hello world
    Hello world
    Hello world
    Hello world
    Hello world

    like 10 times

    Reply
    • @Sravanthi,

      Instead of using echo, why not use printf command to repeat a string or text multiple times to a file shown in the below example.

      # printf 'HelloWorld\n%.0s' {1..5} >> ravi.txt
      # cat ravi.txt 
      HelloWorld
      HelloWorld
      HelloWorld
      HelloWorld
      HelloWorld
      
      Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.