7 echo command uses in Linux with examples

Echo command outputs strings that are passed as arguments and usually used in shell scripts and batch files to output status text to a screen or as a source part of a pipeline. Syntax: echo [-n] [string ...] Let's learn its usage in Linux with practical examples in today's session of Terminal Tuts.

There are several commands of the GNU-Coreutils that practically make the Linux working environment what it is. Some that you might know are cp, mv, rm, cd, etc. These are all fundamental commands that provide minimum requirements to the users. One such command is echo.

Echo command usage in Linux

The echo command does what it means, that is echoing something back. You provide it with input, and it throws that right back at you. But this simple functionality can be twisted to do some more clever things. Let’s check out different possible uses of it.

1. Displaying text

First and most straightforward, showing a line of text that you give to it as input. The easiest example would be:

echo So this is how echo works

Simple usage
Simple usage

As you can see from the result, it merely provides the input with right back. Well, this is simple enough, but in some cases, echo gets confused with punctuation marks. You can see a demo in the screenshot below.

Quotation errors
Quotation errors

To fix this, we can use the double or single quotations (making sure not to confuse echo by adding a single/double quote before the statement ends).

Sorting out the quotation problem
Sorting out the quotation problem

As you can see, the problem does get fixed, but if you enter a quotation mark that you used to start the statement before it ends, echo will not understand it.

2. Creating files, with content

You can create text files with content using a single command using echo. It’s a simple and intuitive command that follows this syntax:

echo "[Text to add to the file]" > [Filename]

Make sure you don’t get confused with the symbols above. Just see the screenshot below for clarification.

Creating files with content in a single command
Creating files with content in a single command

3. Appending text to files

Now, we can also append text to already existing files. This is again a simple command that follows a similar syntax:

echo "[Text to be appended]" > [Filename]

Appending text to files
Appending text to files

4. Listing files

echo can be used as an alternative to ls. While it doesn’t seem much useful, it actually is, because using patterns becomes much more comfortable while using echo to list files. In the simplest form, the command looks like this:

echo *

Listing files
Listing files

Now sorting becomes easier because you can specify the pattern between asterisks, and that will be it. For example, I want to list all files that have the word ‘test’ in the name. The echo version of the command looks like this:

echo *test*

While the ls version is a little more complicated and raises more questions:

ls | grep test

Equivalent ls command
Equivalent ls command

Although, yes, the color-coding and highlighting of ls make the experience better.

5. Working with variables

You can declare and use variables with the on a Linux command line session. A variable can be set in the following way:

[Variable name]="[Variable value]"

Now this variable can be accessed with the echo command. The echo command uses a ‘$’ sign before the variable name to recognize in the command, that it is indeed a variable.

Printing variables
Printing variables

6. Formatting text

Various backslash escape characters are used by the echo command to format text that is to be printed. There are several formatting methods, which are the real essence of the echo command. There is one crucial factor, though, that the -e flag has to be used whenever you want to enable the backslash characters.

We have enlisted some of the most useful characters below:

Newline (\n)

The newline character pushes the text after it to the next line. Example syntax:

echo -e "This is line one, \nand this is line two."

The output looks like:

Tab (\t)

This adds a tab character sized gap where it has been added. Here’s an example syntax:

echo -e "Here comes a tab \tspace."

Output:

Tab
Tab

Vertical tab (\v)

The vertical tab pushes the text after it onto the next line, but the starting position of the text in the next line is exactly after the last character on the previous line.

echo -e "This is vertical \vtab."

It becomes clearer from this example:

Vertical tab
Vertical tab

Backspace (\b)

The backspace character replaces the character right before it with the characters after it (just like a backspace). Example syntax:

echo -e "Complete text. Abruptly ending\btext"

Backspace
Backspace

Carriage Return (\r)

The carriage return is used to change the starting point of the output text. For example:

echo -e "All this text doesn't matter.\rThis is what will be displayed."

Carriage return
Carriage return

Abandon further output (\c)

This is the antithesis to the previous point. This character stops the output of data wherever it is placed. Example:

echo -e "This will be printed. \cAll this will not be displayed."

Abandon output
Abandon output

Alert (\a)

This is the most interesting character, as it emits a sound rather than text formatting. This is rather useful when you’re writing a script for something important that needs to notify you of something. Example syntax:

echo -e "Here comes the bell \a"

NOTE: You have to make sure that you have sound output enabled in your command line application settings to receive the output sound of this command.

7. Integrating commands

This is a significant point that you can display the output of other commands using echo. The distinction is that you have to enclose the command in parentheses for echo to know that it’s an external command. The syntax looks like this:

echo $([Command])

Example:

echo $(ls -a)

The difference between the actual output and the echo’s output is pretty clear.

External command
External command

Conclusion

While on the surface, echo does not seem like a beneficial program; it is an excellent feature for programmers writing real-life usage programs. If you can draw a similarity to Python, in the interpreter, the print function is not very useful. But when you write a whole program in a file, it is one of the most valuable functions. We hope you had a good learning experience.

Pulkit Chandak
Pulkit Chandak is a Linux enthusiast and has been using and experimenting with open source software and hardware too since a long time. He is a huge admirer of open source software and wants to ventilate it to all around him. He is interested in reviewing and writing tutorials on Linux and its many distributions. He believes that freedom in software leads to freedom of the mind from the chains of limits.

1 COMMENT

  1. That part will be a very painful experience :

    3. Appending text to files
    Now, we can also append text to already existing files. This is again a simple command that follows a similar syntax:

    echo “[Text to be appended]” > [Filename]

    Change it to echo
    “[Text to be appended]” >> [Filename]
    please!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,437FansLike
374FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

How to change Hostname on Ubuntu

Generally, a hostname is just an assigned name to a computer, laptop, or any other connected device to a network. You can say a hostname is a computer's nickname, and it is used to identify the machine over the network uniquely.

Manjaro vs. Ubuntu – which is better for you?

If you are a person associated with Computer technology and spend most of the time in the open-source arena, you must have heard or worked with some of the popular Linux distributions we have in the market. Some of the names that you will never miss are; Ubuntu, Arch Linux, Debian, and Mint.

9 Useful Tips Working with Operating System using Python

There are several situations when we want to work with the operating system using python. We may want to see the user details or want to do some tasks with the files and directories. If you are a system administrator, you will find it useful to work with Operating System as one can easily automate some repeating tasks of the Operating System using python.

System76 Lemur Pro Review

Buyers who wish to go for a machine that is based on Linux often show interest in Chromebooks due to the form factor and extended battery life capabilities. Although ChromeOS power these machines, users can still miss out on a more genuine Linux experience. For those who happen to agree, the new Lemur Pro by System76 might get some heads turning.

Top 10 New Features in Linux Kernel 5.8

Linus Torvalds recently announced the release of Linux Kernel 5.8, and he seems delighted with it. He has pointed it out as the most significant release of all time. To developers, this new kernel comes with an addition of 800,000 new code lines and more than 14,000 changed files. To the average user, you might not see many eye-candy changes, as seen in the earlier releases.

How to install Flutter on Linux

Do you want to install Flutter and start creating awesome Android and iOS applications? Then you have come to the right place. Here is a comprehensive tutorial on how to install Flutter on Linux. If you are new to Flutter, don't worry, we got that covered too as we shall go in-depth understanding of Flutter and what it brings to the table.

MUST READ

Buyers who wish to go for a machine that is based on Linux often show interest in Chromebooks due to the form factor and extended battery life capabilities. Although ChromeOS power these machines, users can still miss out on a more genuine Linux experience. For those who happen to agree, the new Lemur Pro by System76 might get some heads turning.
Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.

6 best task managers for Linux

One of the essential tools in any Linux distribution is a Task Manager. It is a system monitor application that gives you a report of all programs running on your computer and the status of your RAM and CPU usage.

Removing the Virus from a Windows PC with a Ubuntu Live USB drive

In this tutorial, we are going to show you how to clean your Windows machine from infected viruses using an Ubuntu live USB or CD and the ClamAV antivirus. The ClamAV is a free, open-source antivirus that can be used on Ubuntu.

3 Best Ways to Uninstall Software on Ubuntu

Uninstallation of programs can be done by graphical way using the Ubuntu Software Center, and the Synaptic Package manager. Command-line way of doing it is also possible using apt-get and aptitude commands. We shall discuss each one of them in detail.

All about Ubuntu editions and which version should you use?

Ubuntu is one of the most popular Linux distributions developed and released by Canonical, and not without reason. It has very enriched repositories, with support for all the programs you could ever need.