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
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.
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).
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.
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]
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:
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:
While the ls version is a little more complicated and raises more questions:
ls | grep test
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.
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:
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:
This adds a tab character sized gap where it has been added. Here’s an example syntax:
echo -e "Here comes a tab tspace."
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:
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 endingbtext"
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."
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."
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 $(ls -a)
The difference between the actual output and the echo’s output is pretty clear.
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.