echo command is perfect for writing formatted text to the terminal window. And it doesn’t have to be static text. It can include shell variables, filenames, and directories. You can also redirect echo to create text files and log files. Follow this simple guide to find out how.
Echo Repeats What You Tell It To Repeat
Zeus was fond of leaving Mount Olympus to consort with beautiful nymphs. On one trip, he told a mountain nymph called Echo to waylay his wife, Hera, if she followed him. Hera did come looking for Zeus, and Echo did all she could to keep Hera in conversation. Finally, Hera lost her temper and cursed poor Echo so that she only repeat the last words that someone else had said. What Hera did to Zeus when she caught up with him is anybody’s guess.
And that, pretty much, is
echo‘s lot in life. It repeats what it has been told to repeat. That’s a simple function, but a vital one. Without
echo , we’d be unable to get visible output from shell scripts, for example.
Whilst not laden down with a multitude of bells and whistles, there’s a good chance that
echo has some capabilities that you didn’t know about or that you’d forgotten.
Most Linux systems provide two versions of
echo. The Bash shell has its own
echo built into it, and there’s a binary executable version of
echo as well.
We can see the two different versions by using the following commands:
type command tells us whether the command we pass to it as its argument is a shell builtin, a binary executable, an alias, or a function. It reports to us that
echo is a shell builtin.
As soon as it has found an answer,
type stops looking for further matches. So it doesn’t tell us if there are other commands with the same name present in the system. But it does tell us which one it finds first. And that’s the one that will be used by default when we issue that command.
whereis command looks for the binary executable, source code, and man page for the command we pass to it as its command-line parameter. It doesn’t look for shell builtins because they don’t have a separate binary executable. They’re an integral part of the Bash executable.
whereis command reports that
echo is a binary executable located in the
To use that version of
echo you would need to explicitly call it by providing the path to the executable on the command line:
The shell builtin doesn’t know what the
--version command-line argument is, it just repeats it in the terminal window:
The examples shown here all use the default version of
echo, in the Bash shell.
Writing Text to the Terminal
To write a simple string of text to the terminal window, type
echo and the string you want it to display:
echo My name is Dave.
The text is repeated for us. But as you experiment, you’ll soon discover that things can get slightly more complicated. Look at this example:
echo My name is Dave and I'm a geek.
The terminal window displays a
> sign and sits there, waiting. Ctrl+C will return you to the command prompt. What happened there?
The single quote or apostrophe in the word “I’m” confused
echo. It interpreted that single quote as the start of a quoted section of text. Because it didn’t detect a closing single quote,
echo was waiting for more input. It expected that further input to include the missing single quote it was waiting for.
To include a single quote in a string, the simplest solution is to wrap the whole string within double quote marks:
echo "My name is Dave and I'm a geek."
Wrapping your text in double quote marks is good general advice. In scripts, it cleanly delimits the parameters you’re passing to
echo. This makes reading—and debugging—scripts much easier.
What if you want to include a double quote character in your string of text? That’s easy, just put a backslash
\ in front of the double quote mark (with no space between them).
echo "My name is Dave and I'm a \"geek.\""
This wraps the word “geek” in double quote marks for us. We’ll see more of these backslash-escaped characters shortly.
Using Variables With echo
So far, we’ve been writing predefined text to the terminal window. We can use variables with
echo to produce output that is more dynamic and has values inserted into it for us by the shell. We can define a simple variable with this command:
A variable called
my_name has been created. It has been assigned the value of the text “Dave.” We can use the variable name in the strings that 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 to let
echo know it is a variable.
There is a caveat. If you’ve wrapped your string in single quote marks
echo will treat everything literally. To have the variable value displayed, and not the name of the variable, use double quote marks.
echo 'My name is $my_name'
echo "My name is $my_name"
Somewhat aptly, that’s worth repeating:
- Using single quote marks results in the text being written to the terminal window in a literal fashion.
- Using double quote marks results in the variable being interpreted—also called variable expansion—and the value is written to the terminal window.
RELATED: How to Work with Variables in Bash
Using Commands With echo
We can use a command with
echo and incorporate its output into the string that is written to the terminal window. We must use the dollar sign
$ as though the command was a variable, and wrap the whole command in parentheses.
We’re going to use the date command. One tip is to use the command on its own before you start using it with
echo. That way, if there is something wrong with the syntax of your command, you identify it and correct it before you include it in the
echo command. Then, if the
echo command doesn’t do what you expect, you’ll know the issue must be with the
echo syntax because you’ve already proven the command’s syntax.
So, try this in the terminal window:
And, satisfied that we’re getting what we expect from the date command, we’ll integrate it into an
echo "Today's date is: $(date +%D)"
Note the command is inside the parentheses and the dollar sign
$ is immediately before the first parenthesis.
Formatting Text With echo
-e (enable backslash escapes) option lets us use some backslash-escaped characters to change the layout of the text. These are the backslash-escaped characters we can use:
- \a: Alert (historically known as BEL). This generates the default alert sound.
- \b: Writes a backspace character.
- \c: Abandons any further output.
- \e: Writes an escape character.
- \f: Writes a form feed character.
- \n: 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\nsplit across three lines\nwith\ttabs\ton\tthe\tthird\tline"
The text is split into a new line where we’ve used the
\n characters and a tab is inserted where we’ve used the
echo -e "Here\vare\vvertical\vtabs"
\n new line characters, a vertical tab
\v moves the text to the line below. But, unlike the
\n new line characters, the
\v vertical tab doesn’t start the new line at column zero. It uses the current column.
\b backspace characters move the cursor back one character. If there is more text to be written to the terminal, that text will overwrite the previous character.
echo -e "123\b4"
The “3” is over-written by the “4”.
\r carriage return character causes
echo to return to the start of the current line and to write any further text from column zero.
echo -e "123\r456"
The “123” characters are overwritten by the “456” characters.
\a alert character will produce an audible “bleep.” It uses the default alert sound for your current theme.
echo -e "Make a bleep\a"
-n (no newline) option isn’t a backslash-escaped sequence, but it does affect the cosmetics of the text layout, so we’ll discuss it here. It prevents
echo from adding a newline to the end of the text. The command prompt appears directly after the text that is written to the terminal window.
echo -n "no final newline"
Using echo With Files and Directories
You can use
echo as a sort of poor man’s version of
ls. Your options are few and far between when you use
echo like this. If you need any kind of fidelity or fine control, you’re better off using
ls and its legion of options.
This command lists all of the files and directories in the current directory:
This command lists all of the files and directories in the current directory whose name starts with “D” :
This command lists all of the “.desktop” files in the current directory:
Yeah. This isn’t playing to
echo‘s strengths. Use
Writing to Files with echo
We can redirect the output from
echo and either create text files or write into existing text files.
If we use the
> redirection operator, the file is created if it does not exist. If the file does exist, the output from
echo is added at the start of the file, overwriting any previous content.
If we use the
>> redirection operator, the file is created if it does not exist. The output from
echo is added to the end of the file and doesn’t overwrite any existing content of the file.
echo "Creating a new file." > sample.txt
echo "Adding to the file." >> sample.txt
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 to the terminal window.
And of course, we can include variables to add some useful information to our file. If the file is a logfile, we might want to have a timestamp added to it. We can do that with the next command.
Note the single quote marks around the parameters for the
date command. They prevent the space between the parameters being interpreted as the end of the parameter list. They ensure the parameters are passed to
echo "Logfile started: $(date +'%D %T')" > logfile.txt
Our logfile is created for us and
cat shows us that the datestamp and timestamp were both added to it.
RELATED: What Are stdin, stdout, and stderr on Linux?
That’s echo’s Repertoire
A simple command, but indispensable. If it didn’t exist, we’d have to invent it.
Zeus’s shenanigans did some good, after all.
- › How to Use Brace Expansion in Linux’s Bash Shell
- › How to Use the wc Command in Linux
- › How to Use the dig Command on Linux
- › How to Customize the Bash Shell With shopt
- › How To Use set and pipefail in Bash Scripts on Linux
- › How to Use the Bash printf Command on Linux
- › 9 Bash Script Examples to Get You Started on Linux
- › How to Run a ChatGPT-like AI on Your Own PC