Wednesday, February 15, 2023
HomeNetworkingSome ways to make use of the echo command on Linux

Some ways to make use of the echo command on Linux


The echo command (a bash built-in) is among the very primary instructions on Linux. As with ls and pwd, you may’t sit on the command line very lengthy with out utilizing it. On the similar time, echo has fairly a couple of makes use of that many people by no means benefit from. So, this publish seems into the numerous methods you should utilize this command.

What’s echo?

Principally, echo is a command that may show any textual content that you simply ask it to show. Nevertheless, if you sort “echo howdy”, the echo command is not solely spitting out these 5 letters, it is really sending out six characters – the final one being a linefeed. Let’s take a look at a few instructions that make this apparent.

First, utilizing echo with no arguments does this:

$ echo

$

Discover that an empty line is displayed earlier than the immediate comes again. In the event you redirect the command’s output to the od -bc command, it’s simple to substantiate that the “invisible” output is just a linefeed – that n or octal 012 within the output under.

$ echo | od -bc
0000000 012
         n
0000001

That n is the rationale that the echo command will also be used so as to add a linefeed to the tip of a file that, for some cause, lacks one. Right here’s a file that lacks a linefeed adopted by the command that provides 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 within the above instance shows the content material of recordsdata in each octal and character format. The output above reveals {that a} linefeed was added to the tip of the file. The od -bc command supplies a really helpful method to study the contents of a file, particularly when utilizing the cat command doesn’t present you every thing it’s essential to see.

> vs >>

One vital factor to concentrate to when utilizing the echo command is that > and >> work with recordsdata in a different way. The > will overwrite the content material of a file, whereas >> will append to it.

echo -n

The echo -n command will omit the linefeed from its output. That is usually utilized in scripts when prompting the person to supply a solution to some query. This works finest when the immediate and the area the place the reply can be entered are on the identical line. For instance, if the person ought to present the identify of the file to be processed by the script, we’re prone to see strains like these:

echo -n "file identify> "
learn file

Whoever runs the script can be prompted to enter a file identify and can sort the file identify as proven right here:

file identify> datafile

Emptying recordsdata

The echo command will also be used to empty recordsdata. Whereas it’s extra frequent to make use of a command akin to cat /dev/null > filename, the command echo -n > filename works as effectively. Each instructions change the content material of a file with … nothing! To show this command into an alias, use a command like this or add it to your .bashrc file.

$ alias empty='echo -n > '

After organising the alias, you may simply sort a command akin to “empty myfile”, and the file will not have content material. Needless to say utilizing >> rather than > would depart the file intact – principally including nothing to the finish of it.

Utilizing echo -e

The echo command’s -e possibility (allow interpretation of backslash escapes) interprets a backslash adopted by another character (e.g., b) in a means that alters the output. For instance, inserting situations of b in a string will trigger the characters previous b to be eliminated (backspaced over). Right here’s an instance:

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

If the textual content contains linefeed (n) characters as a substitute of backspace characters, the command turns the textual content into a number of strains as a substitute – principally creating newlines.

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

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

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

Utilizing t inserts tabs into the output.

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

The person web page for the echo command will present you the entire sequences that you should utilize with the -e possibility.

            backslash
       a     alert (BEL)
       b     backspace
       c     produce no additional output
       e     escape
       f     kind feed
       n     new line
       r     carriage return
       t     horizontal tab
       v     vertical tab
       NNN  byte with octal worth NNN (1 to three digits)
       xHH   byte with hexadecimal worth HH (1 to 2 digits)

Utilizing echo -E rather than echo -e disables escape characters and is default – the identical as echo with out arguments.

Most likely probably the most enjoyable of the echo -e choices is utilizing the v (vertical tabs) possibility. Your command and output will look one thing like this:

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

Altering font colours with echo

You may also change font colours utilizing the echo command. For instance, the command under will show textual content in purple.

$ 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.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments