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,648FansLike
360FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

How to install CMake on Ubuntu

CMake is a cross-platform free and open-source software tool designed to build, test, and package the software. CMake uses a simple platform and compiler-independent configuration files to control the software compilation process.

How to install Lightworks on Ubuntu

Even though Linux may not get a native installer of video editing software like Adobe Premiere or Final Cut Pro, that doesn't mean there are no industry standards tools available. Lightworks is non-linear editing (NLE) video mastering app for Windows, Linux, and macOS. Installing it on Ubuntu is simple due to deb package availability.

How to install DaVinci Resolve on Fedora

Davinci Resolve is a professional application used for color correction, video editing, visual effects, and motion graphics. It is one of the extensively used software by movie industries located in Hollywood.

The 10 Best Programming Languages for Hacking

One of the significant entities we have in Cyber Security is Ethical Hacking (ETH). It is the process of detecting and finding flaws or vulnerabilities in a system that a hacker would exploit.

5 Ways to Open a Terminal in Ubuntu

Even though Ubuntu supports many applications with amazing Graphical User Interfaces (GUI), there are always reasons why users prefer using the Terminal to perform different tasks.

How to install Wine on Fedora Workstation

Linux distributions are becoming more and more popular every day, and Fedora Workstation is not left behind. This popularity brings forth the need to run Windows applications on Linux distros like Fedora. Windows has quite some excellent Software that is not available for Linux.

MUST READ

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.
Elementary OS 5.1 Hera has received a point release with a handful of new features and bug fixes, and we will be reviewing the significant changes in this article. For those new to elementary OS, this Ubuntu-based Linux distribution uses their inhouse built Pantheon desktop environment and AppCenter.

5 Things to do when your Linux system GUI freezes

All operating systems freeze at some point and, Linux distributions are no exceptions. In this post, we are going to look at five methods you can apply to your frozen system before thinking of plugging out the power cable or holding down the power button to power off the PC.

Pop!_OS 20.04 Review: Professional Linux Distribution Ever Made

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.

Scrcpy – Control Android devices from a Linux desktop

Scrcpy is a desktop program that can be used to access your Android phone's system and interface through your computer. The app is quite convenient, and some of its best features are highlighted below.

10 ways to use the Chown command with examples

Any file in a Linux system belongs to a particular owner(s) and a group. It is one of the many features that maintain security in Linux systems. For example; If a file belongs to say UserA, then UserB cannot manipulate the data depending on any additional permissions set unless the user has permission to do that.