Many ways to use the echo command on Linux


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 echo?

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 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
       NNN  byte with octal value NNN (1 to 3 digits)
       xHH   byte with hexadecimal value HH (1 to 2 digits)

Using echo -E in place of echo -e disables escape characters and is default – the same as echo without arguments.

Probably the most fun of the echo -e options is using the v (vertical tabs) option. Your command and output will look something like this:

$ motto5="Whenvlifevhandsvyouvlemons,vmakevlemonade"
$ echo -e $motto5
When
    life
        hands
             you
                lemons,
                       make
                           lemonade

Changing font colors with echo

You can also change font colors using the echo command. For example, the command below will display text in red.

$ echo -e "33[31mThis is some red text"
This is some red text

Use the reset command to set the font color back to normal. That 33[31m string is the code for red. You can also create definitions of the colors using commands such as red=’33[31m‘ since remembering “red” will be a lot easier than remembering 33[31m.

In the example command below, a portion of the text will display in red with a black background, and then the text will return to the terminal window’s normal font and background colors. The echo command will only work, however, after the color red and the reset options have been defined as shown in the first line.

$ red='33[31m'; reset="33[0m"
$ echo -e "${red}This text is red.${reset} This text is not."
This text is red. This text is not.

The ${reset} in the command above returns the screen colors to what they were previously.

Now let’s try my favorite color – purple.

$ purple="33[35m"
$ reset="33[0m"
$ echo -e "${purple}This is a purple text.${reset} And now the text is normal again."
This is a purple text. And now the text is normal again.

I will often source a file that contains the definitions of the colors that I want to use so that I don’t have to define them whenever I want to use them. I add a “. colors” command to any script that needs to display text in some particular color.

$ cat colors
red='33[31m'
blue="33[34m"
purple="33[35m"
white="33[37m"
blackBG='33[40m'
black=';30[0m'
darkgray='1;30m'
pink='33[31m'
green='33[32m'
lightgreen='33[32m'
lightgray='33[37m'
orange="33[33m"
cyan='33[36m'
reset="33[0m"

The commands in the script below will display the phrase “Red, white and blue” in three colors. Can you guess which?

#!/bin/bash

red='33[31m'
white="33[37m"
blue="33[34m"
reset="33[0m"

echo -e "${red}Red, ${white}white and ${blue}blue.${reset}"

Wrap-up

The echo command can do a lot more than display lines of text or add them to the ends of files. I hope some of the uses described in this post will make your hours on the command a little more fun and maybe even more … colorful!

Copyright © 2023 IDG Communications, Inc.



Source link