Basic echo command in Linux with Examples

echo command in Linux

The echo command is one of the basic and most important command when you stepping into the scripting/programming world! Which is used to display or prints the arguments on the terminal. It is a built-in command to all the distributions (operating System) named 'shell or bash'. Mostly the echo command is used by developers or programmers to debug or build products likPlagiarisme websites, applications, Operating Systems, etc.

When it comes to system administration, we can use this 'echo' command to write good shell scripts or batch files. This guide will teach you how to use various options of echo commands. All the below examples of echo commands are tested on RHEL/CENTOS 7.6.

The Global Syntax of echo command with options:

echo [OPTION] [input string]

The following tabular gives you the possible options in echo command:

echo command in linux

 

1. How to simply input a text to get the output using echo command?

# echo LinuxTeck  A complete learning blog

 

Output:- LinuxTeck  A complete learning blog

Note: The above echo command simply printed the specified text to the standard output.

2. How to print a variable?

# x=100
# echo The value of x =$x

 

Output : The value of x = 100

Note: Here the variable name is 'x' and the value assigned to x is '100'. Make sure there is no space between the variable and value. Then we can use the echo to print the value of 'x'.

3. How to make Alert while printing?

# echo -e "LinuxTeck A complete learning blog\a"

 

Output: LinuxTeck A complete learning blog

Note: Here we use '-e' to enable interpretation of backslash escape sequences and '\a' option Alert a beep sound while prints

4. How to add a new line?

# echo -e "LinuxTeck \nA complete learning Blog"

 

Output: LinuxTeck
                 A complete learning Blog

Note: '-e' used an interpretation of backslash escape sequences and '\n' tab used for adding a new line and it will be printed next to the previous line.

5. How to add a horizontal tab in your content?

# echo -e "LinuxTeck \tA \tComplete \tLearning \tBlog"

 

Output: LinuxTeck        A        Complete        Learning        Blog

Note: '-e' used an interpretation of backslash escape sequences and '\t' creates a Horizontal space between the content, it is similar to using the TAB key option from the keyboard.

6. How to add a Vertical tab in your content?

# echo -e "LinuxTeck \vA \vComplete \vLearning \vBlog"

 

Output :    LinuxTeck
                                              A

                                                        Complete

                                                                                Learning

                                                                                                    Blog

Note: '-e' used an interpretation of backslash escape sequences and '\v' creates a Vertical space between the content, it is also similar to using the TAB key option from the keyboard but in vertically.

7. How to suppress the content to display after a certain portion?

# echo -e "LinuxTeck A Complete Learning Blog \cLinux | AWS | VMware | DevOps"

 

Output: LinuxTeck A Complete Learning Blog

Note: '-e' used an interpretation of backslash escape sequences and '\c' option will mainly be used to eliminate the contents. This means it will omit displaying the contents after the '\c' option.

8. How to use Carriage Return?

# echo -e "LinuxTeck is \rA Complete Learning Blog"

 

Output: A Complete Learning Blog

Note: '-e' used an interpretation of backslash escape sequences and '\r' is nothing but to delete the text before it. Means if you want to remove contents from a certain portion to display in the output you can use \r just before it. In the above example, you can see LinuxTeck is been removed in the output.

9. How to use Backspace in your content?

# echo -e "LinuxTeck- \bA \bComplete \bLearning \bBlog"

 

Output : LinuxTeck-ACompleteLearningBlog

Note: '-e' used an interpretation of backslash escape sequences and '\b' eliminate all the spaces in between the content. In the above example you can see the all the spaces have been removed 'A Complete Learning Blog'.

10. How to add Form Feed in your content?

# echo -e "LinuxTeck \fA \fComplete \fLearning \fBlog"

 

Output:    LinuxTeck

                                           A

                                                Complete

                                                                       Learning

                                                                                           Blog

Note: '-e' used an interpretation of backslash escape sequences and '\f' option will separate output pages instead of newlines.

11. How to print all files and folders?

# echo *

 

Output : testfile1 testfile2 testfile3 testfile4 testfolder

Note: This command is similar to the 'ls' .  It will simply print all the files and folders. In 'ls' command we can easily identify the files and folders by colour combination whereas, in echo, it shows only with the plain white colour by default.

12. How to print some particular extensions of files?

#echo *.log

 

Output: boot.log vmware-vmsvc.log wpa_supplicant.log Xorg.0.log Xorg.9.log yum.log

Note: The above example shows how to find the extension of all the .log files.

13. How to redirect the output to a file and nonstandard output?

# echo "LinuxTeck" > test

 

Output : # cat test
LinuxTeck

Note: Using echo with redirect operator to output to a text file and not standard output.

I hope this article will help you to learn a few steps towards echo commands in Linux! This is just a startup to the scripting world. Do practice with various options and it helps you to understand. Drop me your feedback/comments.

Thank you!

If you like the article ? Please share it and help us grow 🙂

Leave a Reply

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