Americas

  • United States
sandra_henrystocker
Unix Dweeb

Many ways to use the echo command on Linux

How-To
Oct 05, 20237 mins
LinuxUnix

The echo command is simple, except when it isn't. Here's a look at the basic command along with some of the more challenging things it can do.

millimeter wave wireless technology sound waves abstract audio graphic
Credit: Getty Images

The echo command (a bash built-in) is one of the very basic commands on Linux. As with ls and pwd, you can’t sit on the command line very long without using it. At the same time, echo has quite a few uses that many of us never take advantage of. So, this post looks into the many ways you can use this command.

What is the echo command on Linux?

Basically, echo is a command that will display any text that you ask it to display. However, when you type “echo hello”, the echo command isn’t only spitting out those five letters, it’s actually sending out six characters – the last one being a linefeed. Let’s look at a couple of commands that make this obvious.

First, using echo with no arguments does this:

$ echo

$

Notice that an empty line is displayed before the prompt comes back. If you redirect the command’s output to the od -bc command, it’s easy to confirm that the “invisible” output is simply a linefeed – that n or octal 012 in the output below.

$ echo | od -bc
0000000 012
         n
0000001

That n is the reason that the echo command can also be used to add a linefeed to the end of a file that, for some reason, lacks one. Here’s a file that lacks a linefeed followed by the command that adds one.

$ od -bc nolinefeed
0000000 150 145 154 154 157
          h   e   l   l   o
0000005
$ echo >> nolinefeed
$ od -bc nolinefeed
0000000 150 145 154 154 157 012
          h   e   l   l   o  n 
0000006

od -bc

The od -bc command in the above example displays the content of files in both octal and character format. The output above shows that a linefeed was added to the end of the file. The od -bc command provides a very useful way to examine the contents of a file, especially when using the cat command doesn’t show you everything you need to see.

> vs >>

One important thing to pay attention to when using the echo command is that > and >> work with files differently. The > will overwrite the content of a file, while >> will append to it.

echo -n

The echo -n command will omit the linefeed from its output. This is often used in scripts when prompting the user to provide an answer to some question. This works best when the prompt and the space where the answer will be entered are on the same line. For example, if the user should provide the name of the file to be processed by the script, we’re likely to see lines like these:

echo -n "file name> "
read file

Whoever runs the script will be prompted to enter a file name and will type the file name as shown here:

file name> datafile

Emptying Linux files

The echo command can also be used to empty files. While it’s more common to use a command such as cat /dev/null > filename, the command echo -n > filename works as well. Both commands replace the content of a file with … nothing! To turn this command into an alias, use a command like this or add it to your .bashrc file.

$ alias empty='echo -n > '

After setting up the alias, you can just type a command such as “empty myfile”, and the file will no longer have content. Keep in mind that using >> in place of > would leave the file intact – basically adding nothing to the end of it.

Using echo -e

The echo command’s -e option (enable interpretation of backslash escapes) interprets a backslash followed by some other character (e.g., b) in a way that alters the output. For example, inserting instances of b in a string will cause the characters preceding b to be removed (backspaced over). Here’s an example:

$ motto="When blife bhands byou blemons, bmake blemonade"
$ echo -e $motto
Whenlifehandsyoulemons,makelemonade

If the text includes linefeed (n) characters instead of backspace characters, the command turns the text into multiple lines instead – basically creating newlines.

$ motto2='When nlife nhands nyou nlemons, nmake nlemonade'
$ echo -e $motto2
When
life
hands
you
lemons,
make
lemonade

Using c will suppress newline characters and, thus, truncate the output.

$ motto3="When life hands you clemons, make lemonade"
$ echo -e $motto3
When life hands you $

Using t inserts tabs into the output.

$ echo $motto4
When life hands you tlemons, tmake lemonade
$ echo -e $motto4
When life hands you     lemons,         make lemonade

The man page for the echo command will show you all of the sequences that you can use with the -e option.

            backslash
       a     alert (BEL)
       b     backspace
       c     produce no further output
       e     escape
       f     form feed
       n     new line
       r     carriage return
       t     horizontal tab
       v     vertical tab
       
sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.