How to use the 'echo' command in Linux

Whether you're new to Linux or have been using the Linux desktop for a long time, there are some commands that don't seem to make sense. This is especially true if you never dive into the command line. But that's where most of the true power of Linux and other Unix-like operating systems works.

One of them is the echo command. At first glance, this may seem like a useless command. But if you dig deeper, you'll find it surprisingly useful.

What does the echo command do?

Looking at the documentation for echo , you won't find them very helpful. It indicates that echo is responsible for 'displays a line of text' . This is probably what you can deduce from the name of the command.

Basically, what echo does is send arguments to standard output along with some basic formatting options. The main reason this command exists is to work within other scripts, allowing you to show the output to the person who is running the script.

The basics

At a basic level, echo does exactly what we can deduce from the command name. Here is an example:

 echo Can anybody hear me 

This command will output 'Can anybody hear me' . If you just type this command, it really looks like an 'echo' exactly what you said (that's why this command is echo ). However, if you add a question mark to the above command, you will get an error. Please type the following:

 echo "Can anybody hear me?" 

The resulting text has no quotation marks around it, but will display the correct question mark. You can also use variables with the echo command.

 x=256 echo $x 

The above command will print the result 256 to the terminal.

More advanced commands

How to use the 'echo' command in Linux Picture 1

The echo command works differently on certain systems. For example, on Linux, there are some options you can't find on other Unix-like operating systems. For example, the -e option allows you to insert characters, like n for newlines or t for tabs.

 echo -e "I sure hope this quote is attributed. nt--Me" 

The above command may look a bit confusing at the end, but it will print something similar to the following:

 I sure hope this quote is attributed. --Me 

You can also use b for Backspace .

Practical examples

As explained, the most practical use case for the echo command is in the scripts you write. You can also use it to tidy up the output of other scripts. For that use case, there are better tools like grep and sed .

Another great use case for the echo command is in slightly modified configuration files. Just use the standard redirect notation > . For example:

 echo "Just some text" >> ~/just-a-file.txt 

This command will append the text to the 'just-a-file.txt' file. Please run it again and this line will appear twice.

As you read through these examples, you may wonder why people use them. They may even make you wonder why people want to use Linux instead of Windows or macOS.

Usually, when something about Linux seems strange, this often originated from its use decades ago. However, this does not necessarily have negative implications. If you're not sure about Linux, see the article: 8 reasons to switch from Windows to Linux to answer your questions.

4.5 ★ | 2 Vote

May be interested

  • 11 uses of ps command in Linux11 uses of ps command in Linux
    for system administrators, ps is a frequently used tool. the ps command is used to list the processes currently running on the system, with many available filtering and display modes via flags and arguments.
  • Instructions for using zforce command on LinuxInstructions for using zforce command on Linux
    the gzip command is a popular tool used to compress / decompress files in linux. tipsmake.com presents the basics of this tool in the article: some basic terminal commands in linux mint 11.
  • How to limit access to su command in LinuxHow to limit access to su command in Linux
    if you have added linux to your data center or are just using a single linux machine for your business, you need to make sure it is as secure as possible.
  • 12 things Linux is easier to do in the command line than graphical software12 things Linux is easier to do in the command line than graphical software
    graphical user interfaces (guis) were invented to make life easier for computer users, so it's very common to use them to perform most everyday tasks.
  • 11 df commands in Linux (with example)11 df commands in Linux (with example)
    with the df command in linux, you can easily check your system for details about disk space and file size. these 12 examples of df commands will show you how to do them in detail.
  • How to use the Linux command line on Android with TermuxHow to use the Linux command line on Android with Termux
    android is a very operating system 'capacity with more and more desktop accessibility applications. however, sometimes you want to make some things on android that can be as easy as desktop. fortunately, you can use the termux tool, which builds on the existing infrastructure and provides a command line environment that allows you to install real linux applications on your android device.
  • Instructions for using find command in LinuxInstructions for using find command in Linux
    the find command is one of the most important and handy commands on a linux system. as its name suggests, the command can find files on a linux pc based on a variety of conditions and variables you set.
  • Instructions for making a call using Amazon EchoInstructions for making a call using Amazon Echo
    want alexa to call friends in your contacts or leave a message for mom on amazon echo? here's how to use this smart speaker.
  • How to use the history command in LinuxHow to use the history command in Linux
    as you spend more and more time in terminal sessions, you will constantly find new commands that make everyday tasks more efficient. the gnu history command is one of them.
  • Instructions for using pstree command on LinuxInstructions for using pstree command on Linux
    pstree is a powerful and useful command to display processes running in linux. like the ps command, it shows all the processes that are currently active on your login system. the main difference is that when running the pstree command, processes are organized into tree sorting instead of lists like using the ps command.