Tips on how to Use the Echo Command on Linux

Tips on how to Use the Echo Command on Linux

All

Linux Articles / All 38 Views comments

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

The echo command is ideal for writing formatted textual content to the terminal window. And it doesn’t need to be static textual content. It may well embrace shell variables, filenames, and directories. It's also possible to redirect echo to create textual content information and log information. Comply with this easy information to learn how.

Echo Repeats What You Inform It To Repeat

Zeus was keen on leaving Mount Olympus to consort with lovely nymphs. On one journey, he informed a mountain nymph referred to as Echo to waylay his wife, Hera, if she adopted him. Hera did come in search of Zeus, and Echo did all she might to maintain Hera in dialog. Lastly, Hera misplaced her mood and cursed poor Echo in order that she solely repeat the final phrases that another person had stated. What Hera did to Zeus when she caught up with him is anyone’s guess.

And that, just about, is echo‘s lot in life. It repeats what it has been told to repeat. That’s a easy perform, however an important one. With out echo , we’d be unable to get seen output from shell scripts, for instance.

While not laden down with a mess of bells and whistles, there’s a great probability that echo has some capabilities that you simply didn’t find out about or that you simply’d forgotten.

echo? echo!

Most Linux methods present two variations of echo. The Bash shell has its personal echo constructed into it, and there’s a binary executable model of echo as nicely.

We will see the 2 totally different variations through the use of the next instructions:

sort echo
whereis echo

type echo in a terminal window

The sort command tells us whether the command we pass to it as its argument is a shell builtin, a binary executable, an alias, or a perform. It stories to us that echo is a shell builtin.

As quickly because it has discovered a solution, sort stops in search of additional matches. So it doesn’t inform us if there are different instructions with the identical identify current within the system. Nevertheless it does inform us which one it finds first. And that’s the one which will probably be utilized by default once we problem that command.

The whereis command seems for the binary executable, supply code, and man web page for the command we cross to it as its command-line parameter. It doesn’t search for shell builtins as a result of they don’t have a separate binary executable. They’re an integral a part of the Bash executable.

The whereis command studies that echo is a binary executable situated within the /bin listing.

To make use of that model of echo you would wish to explicitly name it by offering the trail to the executable on the command line:

/bin/echo --version

/bin/echo --version in a terminal window

The shell builtin doesn’t know what the --version command-line argument is, it simply repeats it within the terminal window:

echo --version

echo --version in a terminal window

The examples proven right here all use the default model of echo, within the Bash shell.

Writing Textual content to the Terminal

To write down a easy string of textual content to the terminal window, sort echo and the string you need it to show:

echo My identify is Dave.

echo My name is Dave. in a terminal window

The textual content is repeated for us. However as you experiment, you’ll quickly uncover that issues can get barely extra difficult. Take a look at this instance:

echo My identify is Dave and I am a geek.

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

The terminal window shows a  > signal and sits there, ready. Ctrl+C will return you to the command immediate. What occurred there?

The only quote or apostrophe within the phrase “I’m” confused echo. It interpreted that single quote as the beginning of a quoted part of textual content. As a result of it didn’t detect a closing single quote, echo was ready for extra enter. It anticipated that additional enter to incorporate the lacking single quote it was ready for.

To incorporate a single quote in a string, the only answer is to wrap the entire string inside double quote marks:

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

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

Wrapping your textual content in double quote marks is sweet basic recommendation. In scripts, it cleanly delimits the parameters you’re passing to echo. This makes studying—and debugging—scripts a lot simpler.

What if you wish to embrace a double quote character in your string of textual content? That’s straightforward, simply put a backslash in entrance of the double quote mark (with no area between them).

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

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

This wraps the phrase “geek” in double quote marks for us. We’ll see extra of those backslash-escaped characters shortly.

Utilizing Variables With echo

Up to now, we’ve been writing predefined textual content to the terminal window. We will use variables with echo to supply output that's extra dynamic and has values inserted into it for us by the shell. We will outline a easy variable with this command:

my_name="Dave"

A variable referred to as my_name has been created. It has been assigned the worth of the textual content “Dave.” We will use the variable identify within the strings that we move to echo , and the worth of the variable will probably be written to the terminal window. You have to put a greenback signal $ in entrance of the variable identify to let echo know it's a variable.

There's a caveat. Should you’ve wrapped your string in single quote marks echo will deal with every little thing actually. To have the variable worth displayed, and never the identify of the variable, use double quote marks.

echo 'My identify is $my_name'
echo "My identify is $my_name"

echo 'My name is $my_name' in a terminal window

Considerably aptly, that’s value repeating:

  • Utilizing single quote marks leads to the textual content being written to the terminal window in a literal style.
  • Utilizing double quote marks leads to the variable being interpreted—additionally referred to as variable enlargement—and the worth is written to the terminal window.

RELATED: How to Work with Variables in Bash

Utilizing Instructions With echo

We will use a command with echo and incorporate its output into the string that's written to the terminal window. We should use the greenback signal $ as if the command was a variable, and wrap the entire command in parentheses.

We’re going to make use of the date command. One tip is to make use of the command by itself earlier than you begin utilizing it with echo. That approach, if there's something fallacious with the syntax of your command, you determine it and proper it earlier than you embrace it within the echo command. Then, if the echo command doesn’t do what you anticipate, you’ll know the difficulty have to be with the echo syntax since you’ve already confirmed the command’s syntax.

So, do this within the terminal window:

date +%D

date +%D in a terminal window

And, glad that we’re getting what we anticipate from the date command, we’ll combine it into an echo command:

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

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

Notice the command is contained in the parentheses and the greenback signal $ is instantly earlier than the primary parenthesis.

Formatting Textual content With echo

The -e (allow backslash escapes) choice lets us use some backslash-escaped characters to vary the format of the textual content. These are the backslash-escaped characters we will use:

  • a: Alert (traditionally often known as BEL). This generates the default alert sound.
  • b: Writes a backspace character.
  • c: Abandons any additional output.
  • e: Writes an escape character.
  • f: Writes a type feed character.
  • n: Writes a brand new line.
  • r: Writes a carriage return.
  • t: Writes a horizontal tab.
  • v: Writes a vertical tab.
  • \: Writes a backslash character.

Let’s use a few of them and see what they do.

echo -e "This can be a lengthy line of textnsplit throughout three linesnwithttabstontthetthirdtline"

echo -e "This is a long line of textnsplit across three linesnwithttabstontthetthirdtline" in a terminal window

The textual content is cut up into a brand new line the place we’ve used the n characters and a tab is inserted the place we’ve used the t characters.

echo -e "Herevarevverticalvtabs"

echo -e "Herevarevverticalvtabs" in a terminal window

Just like the n new line characters, a vertical tab v strikes the textual content to the road under. However, in contrast to the n new line characters, the v vertical tab doesn’t begin the brand new line at column zero. It makes use of the present column.

The b backspace characters transfer the cursor again one character. If there's extra textual content to be written to the terminal, that textual content will overwrite the earlier character.

echo -e "123b4"

echo -e "123b4" in a terminal window

The “three” is over-written by the “four”.

The r carriage return character causes echo to return to the beginning of the present line and to write down any additional textual content from column zero.

echo -e "123r456"

echo -e "123r456" in a terminal window

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

The a alert character will produce an audible “bleep.” It makes use of the default alert sound in your present theme.

echo -e "Make a bleepa"

echo -e "Make a bleepa" in a terminal window

The -n (no newline) choice isn’t a backslash-escaped sequence, nevertheless it does have an effect on the cosmetics of the textual content format, so we’ll talk about it right here. It prevents echo from including a newline to the top of the textual content. The command immediate seems immediately after the textual content that's written to the terminal window.

echo -n "no ultimate newline"

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

Utilizing echo With Information and Directories

You should use echo as a kind of poor man’s model of ls. Your choices are few and much between whenever you use echo like this. For those who want any sort of constancy or superb management, you’re higher off utilizing ls and its legion of options.

This command lists all the information and directories within the present listing:

echo *

This command lists all the information and directories within the present listing whose identify begins with “D” :

echo D*

This command lists all the “.desktop” information within the present listing:

echo *.desktop

echo * in a terminal window

Yeah. This isn’t enjoying to echo‘s strengths. Use ls.

Writing to Information with echo

We will redirect the output from echo and both create textual content information or write into present textual content information.

If we use the > redirection operator, the file is created if it doesn't exist. If the file does exist, the output from echo is added at first of the file, overwriting any earlier content material.

If we use the >> redirection operator, the file is created if it doesn't exist. The output from echo is added to the top of the file and doesn’t overwrite any present content material of the file.

echo "Creating a brand new file." > pattern.txt
echo "Including to the file." >> pattern.txt
cat pattern.txt

echo "Creating a new file." > sample.txt in a terminal window

A brand new file is created by the primary command, and textual content is inserted into it. The second command provides a line of textual content to the underside of the file. The cat command shows the contents of the file to the terminal window.

And naturally, we will embrace variables so as to add some helpful info to our file. If the file is a logfile, we'd need to have a timestamp added to it. We will do this with the subsequent command.

Word the only quote marks across the parameters for the date command. They forestall the area between the parameters being interpreted as the top of the parameter listing. They make sure the parameters are handed to date appropriately.

echo "Logfile began: $(date +'%D %T')" > logfile.txt
cat logfile.txt

echo "Logfile started: $(date +'%D %T')" > logfile.txt in a terminal window

Our logfile is created for us and cat exhibits us that the datestamp and timestamp have been each added to it.

RELATED: What Are stdin, stdout, and stderr on Linux?

That’s echo’s Repertoire

A easy command, however indispensable. If it didn’t exist, we’d should invent it.

Zeus’s shenanigans did some good, in any case.

Comments