Magazine

15 Practical Examples of Echo Command

Posted on the 18 June 2020 by Satish Kumar @satish_kumar86

If you are writing a bash script, you know echo command very well. It is use to display text lines on an output screen or a file.

The echo command is the most common command used by all Linux users, and it is built-in Linux command.

echo command syntax:
echo [option(s)] [string(s)]

Examples:

Here, we list out some of the practical examples of the echo command. You will get the idea about “How to use the echo command in Linux or bash script?”.

Print a line of text on standard output screen

To print any text on the output screen, use echo command with the text string.

# echo "LinuxConcept Provides Linux tutorial for Linux Administrator"
Output on the Screen:
LinuxConcept Provides Linux tutorial for Linux Administrator

Display or print the value of variables

Use the echo command to display or print the value of variables. For example, declare a variable of z and assign its value 25.

# z=25
# echo "The value of variable Z = $z"
Output:
The value of variable z = 25

Use “\b” with backslash interpretor “-e”

We can use “\b” option with backslash interpretor “-e” to remove all spaces in between.

# echo -e "LinuxConcept \bProvides \bLinux \btutorial \bfor \bLinux \bAdministrator"
Output:
LinuxConceptProvidesLinuxtutorialforLinuxAdministrator

Use “\n” with backslash interpretor “-e”

We can use “\b” option with backslash interpretor “-e” to use new line where it is used.

# echo -e "LinuxConcept \nProvides \nLinux \ntutorial \nfor \nLinux \nAdministrator"
Output:
LinuxConcept 
Provides 
Linux 
tutorial 
for 
Linux 
Administrator"

Use “\t” with backslash interpretor “-e”

We can use “\t” option with backslash interpretor “-e” to use horizontal tab spaces where it is used.

# echo -e "LinuxConcept \tProvides \tLinux \ttutorial \tfor \tLinux \tAdministrator"
Output:
LinuxConcept    Provides        Linux   tutorial        for     Linux   Administrator

Use “\t” and “\n”  simultaneously

The echo command gives freedom to use the newline “\n” and horizontal tab space “\t” option simultaneously like below:

# echo -e "LinuxConcept \n\tProvides \n\tLinux \n\ttutorial \n\tfor \n\tLinux \n\tAdministrator"
Output:
LinuxConcept
        Provides
        Linux
        tutorial
        for
        Linux
        Administrator

Use “\v” with backslash interpretor “-e”

We can use “\t” option with backslash interpretor “-e” to use vertical tab spaces where it is used.

# echo -e "LinuxConcept \vProvides \vLinux \vtutorial \vfor \vLinux \vAdministrator"
Output:
LinuxConcept
             Provides
                      Linux
                            tutorial
                                     for
                                         Linux
                                               Administrator

Use “\v” and “\n”  simultaneously

The echo command gives freedom to use the newline “\n” and vertical tab space “\v” option simultaneously like below:

# echo -e "LinuxConcept \n\vProvides \n\vLinux \n\vtutorial \n\vfor \n\vLinux \n\vAdministrator"
Output:
LinuxConcept

Provides

Linux

tutorial

for

Linux

Administrator

Use “\r” with backslash interpretor “-e”

We can use “\r” option with backslash interpretor “-e” to use carriage return in output where it is used.

# echo -e "LinuxConcept \rProvides \rLinux \rtutorial \rfor \rLinux \rAdministrator"
Output:
Administrator

Use “\c” with backslash interpretor “-e”

We can use “\c” option with backslash interpretor “-e” to emitting newline  in output where it is used.

# echo -e "LinuxConcept Provides Linux tutorial \cfor Linux Administrator"
Output
LinuxConcept Provides Linux tutorial [[email protected]]#

Use “-n” option with echo

We can use the “-n” option with echo command to trailing newline.

# echo -n "LinuxConcept Provides Linux tutorial for Linux Administrator"
Output:
LinuxConcept Provides Linux tutorial for Linux Administrator[[email protected]]#

Use “\a” with backslash interpretor “-e”

We can use “\a” option with backslash interpretor “-e” to use alert (sound alert) where it is used.

# echo -e "LinuxConcept Provides Linux tutorial \afor Linux Administrator"
Output:
LinuxConcept Provides Linux tutorial for Linux Administrator

Use as ‘ls’ command alternative

We can use echo command with ‘*’ to display all files/folders in the current directory like ls command.

# echo *
Output:
123.txt abc xyz test.png satish.out

Display specific types of files

We can use echo to display specific types of files in the current directory. For example, we want to show only ‘.jpg’ image files in the current directory; we can view only ‘.jpg’ files using below command:

# echo *.jpg
Output:
testing.jpg output.jpg 123.jpg

echo output redirect

We can also redirect the echo test into the file instead of the standard output.

# echo "LinuxConcept is good for me." > mesg.txt

To check the redirect output read the mesg.txt file

# cat mesg.txt
Output:
LinuxConcept is good for me.

Last Word:

There are lots of ways we can use a single command as per our needs. Here I try to capture all practical examples of echo command what we are using every day. If you think some of the other examples also need to capture in this list, please let us know using the comment.

If You Like What We Do Here On LinuxConcept, You Should Consider:

Stay Connected to: Twitter | Facebook

Subscribe to our email updates: Sign Up Now

We are thankful for your support.


You Might Also Like :

Back to Featured Articles on Logo Paperblog

These articles might interest you :