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.
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.
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.
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
x=256 echo $x
The above command will print the result 256 to the terminal.
More advanced commands
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
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
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.