How to Use the Echo Command on Linux

A Linux terminal window on an Ubuntu-themed desktop.Fatmawati Achmad Zaenuri / Shutterstock

The echo command is perfect for writing formatted text in the terminal window. And it doesn't have to be static text. It can include shell variables, file names, and directories. You can also redirect the echo to create text files and log files. Follow this simple guide to find out how.

Echo repeats what you tell him to repeat

Zeus loved to leave Mount Olympus to meet beautiful nymphs. On a trip, he told a mountain nymph called Echo waylay his wife, Hera, if she followed him. Hera came to get Zeus, and Echo did everything she could to keep Hera in conversation. Finally, Hera lost her temper and cursed poor Echo so that she would only repeat the last words that someone else had said. What Hera did to Zeus when she caught up with him is a guess.

And that, roughly, echoes life. He repeats what he was told to repeat. It’s a simple, but vital function. Without echo, we could not get visible output from shell scripts, for example.

Although it is not loaded with a multitude of bells and whistles, there is a good chance that the echo has abilities that you did not know or that you had forgotten.

echo? echo!

Most Linux systems provide two versions of echo. the Bash shell has its own built-in echo, and there is also a binary echo executable version.

We can see the two different versions using the following commands:

echo type
where is the echo

type the echo in a terminal window

The standard command tells us if the order we place with him like his argument is a built-in shell, binary executable, alias or function. He reports that the echo is a built-in shell.

As soon as he finds an answer, the guy stops looking for other matches. So it doesn't tell us if there are other commands of the same name in the system. But he tells us which one he finds first. And this is the one that will be used by default when we issue this command.

The whereis command searches for the binary executable, source code, and manual page for the command, which we pass to it as a command line. setting. It does not look for internal shell commands because they do not have a separate binary executable. They are an integral part of the Bash executable.

The whereis command reports that echo is a binary executable located in the / bin directory.

To use this echo version, you must explicitly call it by providing the path to the executable on the command line:

/ bin / echo –version

/ bin / echo --version in a terminal window

The built-in shell doesn't know what the command line argument –version is, it just repeats it in the terminal window:

echo –version

echo --version in a terminal window

The examples shown here all use the default version of echo, in the Bash shell.

Writing text on the terminal

To write a simple text string in the terminal window, type echo and the string you want it to display:

echo My name is Dave.

echo My name is Dave. in a terminal window

The text is repeated for us. But by experimenting, you will soon discover that things can get a little more complicated. Look at this example:

echo My name is Dave and I am a geek.

echo My name is Dave and I am a geek. in a terminal window

The terminal window displays a> sign and stays there, waiting. Ctrl + C will return you to the command prompt. What happened over there?

The single quote or apostrophe in the word "I am" echoes. He interpreted this unique quote as the start of a quoted section of text. Because it did not detect a single closing quotation mark, the echo was waiting for more comments. He expected other contributions to include the missing single quote he was expecting.

The easiest way to include a single quote in a string is to wrap the entire string in double quotes:

echo "My name is Dave and I am a geek."

echo "My name is Dave and I am a geek." in a terminal window

Wrapping your text in double quotes is good general advice. In scripts, it properly delimits the parameters you pass to the echo. This makes it easier to read and debug scripts.

What if you want to include a double quote character in your text string? It's easy, just put a backslash in front of the double quotation mark (no space between them).

echo "My name is Dave and I'm a geek. ""

echo "My name is Dave and I am a" geek.  "" in a terminal window

That puts the word "geek" in double quotes for us. We will see more of these escaped backslash characters shortly.

Using echo variables

So far, we have written predefined text in the terminal window. We can use echo variables to produce more dynamic output and with values ​​inserted for us by the shell. We can define a simple variable with this command:

my_name = "Dave"

A variable called my_name has been created. The value of the text "Dave" has been assigned to it. We can use the name of the variable in the strings we pass to echo, and the value of the variable will be written to the terminal window. You must put a dollar sign $ in front of the variable name so that the echo knows that it is a variable.

There is a caveat. If you've put your string in single quotes, the echo will literally process everything. To display the value of the variable and not the name of the variable, use double quotes.

echo & # 39; My name is $ my_name & # 39;
echo "My name is $ my_name"

echo & # 39; My name is $ my_name & # 39; in a terminal window

It is rightly worth repeating:

Using single quotes results in literal writing of the text in the terminal window.
The use of double quotes results in the interpretation of the variable, also called variable expansion, and the value is written to the terminal window.

RELATED: How to work with variables in Bash

Using echo commands

We can use an echo command and embed its output in the string that is written in the terminal window. We must use the dollar sign $ as if the command were a variable and wrap the entire command in parentheses.

We will use the order date. One tip is to use the command alone before you start using it with echo. This way, if there is a problem with the syntax of your command, you identify it and correct it before including it in the echo command. Then, if the echo command does not do what you expect, you will know that the problem must be with the echo syntax, because you have already proven the syntax of the command.

So try this in the terminal window:

date +% D

date +% D in a terminal window

And, convinced that we get what we expect from the date command, we will integrate it into an echo command:

echo "Today's date is: $ (date +% D)"

echo "Today's date is: $ (date +% D)" in a terminal window

Note that the command is inside the parentheses and that the dollar sign $ is immediately before the first parenthesis.

Text formatting with echo

The -e option (enable backslash escapements) allows us to use certain characters escaped by a backslash to change the presentation of the text. These are the escape characters with backslash that we can use:

a: Alert (historically known as BEL). This generates the default alert sound.
b: Writes a backspace character.
vs: Abandon any additional exits.
e: Writes an escape character.
F: Writes a form flow character.
not: Writes a new line.
r: Writes a carriage return.
t: Writes a horizontal tab.
v: Writes a vertical tab.
\: Writes a backslash character.

Let's use some and see what they do.

echo -e "This is a long line of text n split into three lines navec ttabs ton t ththird tline"

echo -e "This is a long line of text  n split into three lines  navec  ttabs  ton  t   the third  tline" in a terminal window

The text is divided into a new line where we have used the characters n and a tab is inserted where we have used the characters t.

echo -e "Here vare vvertical vtabs"

echo -e "Here  vare  vvertical  vtabs" in a terminal window

Like the n new line characters, a vertical tab v moves the text to the line below. But, unlike n new line characters, the vertical tab v does not start the new line at column zero. It uses the current column.

The backspace characters b move the cursor back one character. If there is more text to write on the terminal, this text will overwrite the previous character.

echo -e "123 b4"

echo -e "123  b4" in a terminal window

The “3” is replaced by the “4”.

The carriage return character r returns the echo to the start of the current line and writes any additional text from the zero column.

echo -e "123 r456"

echo -e "123  r456" in a terminal window

The characters “123” are replaced by the characters “456”.

The a alert character will produce an audible "beep". It uses the default alert sound for your current theme.

echo -e "Beep a"

The -n option (no new line) is not a backslash sequence, but it does affect the text layout cosmetics, so let's discuss it here. It prevents echo from adding a new line at the end of the text. The command prompt appears directly after the text written in the terminal window.

echo -n "no new final line"

echo -n "no final newline" in a terminal window

Using echo with files and directories

You can use echo as a kind of poor version of ls. Your options are scarce when using echo like this. If you need fidelity or precise control, it's best to use ls and his legion of options.

This command lists all the files and directories in the current directory:

echo *

This command lists all the files and directories in the current directory whose name begins with "D":

echo D *

This command lists all ".desktop" files in the current directory:

echo * .desktop

echo * in a terminal window

Yeah. This does not play to echo the forces. Use ls.

Write to echo files

We can redirect the echo output and create text files or write to existing text files.

If we use the> redirect operator, the file is created if it does not exist. If the file exists, the echo output is added to the start of the file, overwriting any previous content.

If we use the redirect operator >>, the file is created if it does not exist. Echo output is added to the end of the file and does not overwrite any existing content in the file.

echo “Creating a new file.” > sample.txt
echo "Add to file." >> sample.txt
cat sample.txt

sample.txt in a terminal window "width =" 646 "height =" 167 "onload =" pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this); "onerror =" this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this); "/>

A new file is created by the first command and text is inserted into it. The second command adds a line of text to the bottom of the file. The cat command displays the contents of the file in the terminal window.

And of course we can include variables to add useful information to our file. If the file is a log file, we might want to add a time stamp to it. We can do this with the following command.

Note the single quotes around the date command parameters. They prevent the space between parameters from being interpreted as the end of the parameter list. They make sure that the parameters are passed to date correctly.

echo “Logfile started: $(date +’%D %T’)” > logfile.txt
cat logfile.txt

logfile.txt in a terminal window "width =" 646 "height =" 132 "onload =" pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this); "onerror =" this.onerror = null; pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon (this); "/>

Our log file is created for us and cat shows us that the timestamp and the timestamp have both been added to it.

RELATED: What are stdin, stdout and stderr under Linux?

This is the repertoire of the echo

A simple order, but essential. If it did not exist, it would have to be invented.

Zeus' shenanigans did good after all.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.