How to Use the Echo Command on Linux

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

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

The echo repeats what you tell him to repeat

Zeus liked to leave Mount Olympus with beautiful nymphs. On a trip, he told a mountain nymph named Echo: stall his wifeHera, if she followed him. Hera came for Zeus and Echo did everything in his power to keep Hera in conversation. Finally, Hera is angered and cursed poor Echo for her to repeat only the last words spoken by someone else. What Hera did to Zeus when she caught him, did not he?

And that's pretty much the echo of life. He repeats what it was said to repeat. It's a simple but essential function. Without echo, we could not get any visible output from shell scripts, for example.

Although not loaded with a multitude of bells and whistles, there is a good chance that the echo has features that you do not know or have forgotten.

echo? echo!

Most Linux systems provide two versions of echo. the Shell bash has its own built-in echo and a binary executable version of the echo.

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

type echo
where is the echo

type echo in a terminal window

The typical command tells us if we place the order like his argument is an embedded shell, a binary executable, an alias, or a function. It tells us that echo is an integrated shell.

As soon as he has found an answer, the guy stops looking for other matches. So, that does not 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 execute this command.

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

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

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

/ bin / echo –version

/ bin / echo --version in a terminal window

The built-in shell does not 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.

Write text on the terminal

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

echo I'm calling Dave.

echo I'm calling 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 I'm calling Dave and I'm a geek.

echo I'm calling Dave and I'm a geek. in a terminal window

The terminal window displays a> sign and sits on hold. Ctrl + C will take you back to the command prompt. What happened there?

The simple quote or apostrophe of the word "I am" is a confused echo. He interpreted this unique quote as the beginning of a quoted text section. Since he did not detect a single closing quote, echo was waiting for more information. She expected other contributions to include the missing single citation expected.

To include a single quotation mark in a string, the simplest solution is to wrap the entire string in quotation marks:

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

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

Wrap your quotation marks is a good general advice. In scripts, it properly delimits the parameters you pass to echo. This makes reading and debugging scripts a lot easier.

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

echo "I'm calling Dave and I'm a" geek "."

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

This wraps the word "geek" in quotation marks. We will soon see more characters escaped by a backslash.

Using variables with echo

Until now, we are writing predefined text in the terminal window. We can use echo variables to produce a more dynamic output and for which values ​​are inserted 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 him. We can use the name of the variable in the strings we pass to echo, and the value of the variable will be written in the terminal window. You have to put a dollar sign in front of the name of the variable for the echo to know that it's about a variable.

There is a warning. If you have surrounded your string with single quotes, echo will treat everything literally. For the value of the variable to be displayed, not the name of the variable, use the double quotation marks.

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

echo my name is $ myname & # 39; in a terminal window

It is worth repeating:

The use of single quotation marks causes literal writing of the text in the terminal window.
The use of double quotes results in the interpretation of the variable, also called variable extension, and the value is written in the terminal window.

RELATED, RELATED, RELATED: How to work with variables in Bash

Using commands with echo

We can use a command with echo and embed its output in the string written in the terminal window. We need to use the dollar sign as if the command was a variable and put the entire command in parentheses.

We will use the date order. One trick is to use the command itself before starting to use it with echo. Thus, 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 related to the echo syntax because you have already proved 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 "The current date is: $ (date +% D)"

echo "The date for today is: $ (date +% D)" in a terminal window

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

Format text with echo

The -e option (enable backslash escaping) allows us to use some characters escaped by backslashes to change the text presentation. Here 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 subsequent release.
e: Writes an escape character.
F: Writes a form feed 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 of them and see what they do.

echo -e "This is a long line of text n split on three lines n with ttabs ton tlast line"

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

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

echo -e "Here vare vvertical vtabs"

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

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

The backspace characters b move the cursor one character backwards. 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 overwritten by the "4".

The wrap character r allows echo to return to the beginning of the current line and write any other text from the zero column.

echo -e "123 r456"

echo -e "123  r456" in a terminal window

The characters "123" are overwritten by the characters "456".

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

echo -e "Make a bleep "

The -n option (no new line) is not a backslash escaped sequence, but it affects the aesthetic appearance of the text layout; we will discuss it here. This prevents the 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

Use echo with files and directories

You can use echo as a kind of ls version by a poor man. Your options are rare when you use an echo like this. If you need accurate fidelity or control, use ls and its 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 the ".desktop" files in the current directory:

echo * .desktop

echo * in a terminal window

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

Write in files with echo

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

If we use the operator> redirection, the file is created if it does not exist. If the file exists, the echo output is added to the beginning of the file, replacing the previous content.

If we use the redirection operator >>, the file is created if it does not exist. The echo output is added to the end of the file and does not overwrite the existing contents of the file.

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

sample.txt in a terminal window "width =" 646 "height =" 167 "src =" / pagespeed_static / 1.JiBnMqyl6S.gif "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 may want to add a timestamp. We can do it with the following command.

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

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

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

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

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

The echo directory

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

Zeus's shenanigans have done 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.