Echo Command in Linux

FREE Online Courses: Elevate Skills, Zero Cost. Enroll Now!

In this article, we will learn everything about the echo command in Linux. We will look at what the echo command is, what it is used for, a brief history of it, and the syntax and options used with the echo command. In the end, we will look into some practical examples of the echo command in the terminal of Ubuntu 20.04, So pay attention and read till the end.

What is the echo command?

As the name suggests, the echo command is a command-line-based utility in Linux-based operating systems that helps display a line of text. The echo command is most commonly used in shell programs than on the terminal.

Different programming languages have different commands to print out a statement. For example, Python has “printf”, C programming language has “printf”, etc. Similarly, what printf is to C, echo is to Shell scripting.

Apart from Linux distributions, the echo command is also available in operating systems like SymbOS, KolibriOS, HP MPE/iX, ReactOS, Microsoft Windows, IBM OS/2, Digital Research FlexOS, Acorn Computers Panos, Microwave OS-9, Zilog Z80-RIO, MetaComCo TRIPOS, TSC FLEX, Multics, Unix-like and Unix operating systems.

In a nutshell, the echo command is used to print characters, variables or even simple lines of text on the screen.

A brief history of the echo command

The echo command first started in Multics and was later specified by Doug Mcllroy in C language as the “finger exercise” and verified to be helpful. The echo command made its very first appearance in the second version of Unix.

Echo command later began developing escape sequences similar to C programming languages like \n and \t, along with the difference that the octal escape sequences were represented as the \0ooo rather than \ooo in C.

Syntax of the echo command

The syntax of the echo command is very simple. See for yourself:

echo <options> <text>

Let us look at the field present in the syntax of the echo command:

1. <options>

This field takes in a range of options that specify how the echo command should function and print the output.

2. <text>

This field takes in the text that you want to print. In this field, you can specify characters, words, variables, or simple lines of text.

Options used with Linux echo command

Unlike the typical Linux fashion, where every command has a truckload of options, echo has only five options. Let us look at them:

1. -n

This option does not output a trailing newline.

2. -e

This option enables the interpretation of backslash escape sequences. We will look into these escape sequences in the next section.

3. -E

This option disables the interpretation of backslash escape sequences. This option is already selected by default.

4. –help

This option displays the help menu of the echo command.

5. –version

This option shows the version of the echo command you are using.

Escape sequences used with Linux echo command

We have seen that the echo command has escape sequences like “\n” and “\t” we have also seen that we can use such escape sequences when we pair the echo command with the option “-e” let us look at the different escape sequences and what they are used for.

1. \\

This escape sequence lets you print a literal backslash (\) in your text.

2. \a

This escape sequence gives you an alert (the BELL character).

3. \b

This escape sequence stands for a backspace.

4. \c

This escape sequence produces no further output after this.

5. \e

This escape sequence stands for escape (Esc).

6. \f

This escape sequence gives a form feed.

7. \n

This escape sequence gives a new line. It means the text after this will appear in a new line and not in the same line before it.

8. \r

This escape sequence gives a carriage return.

9. \t

This escape sequence prints a horizontal tab (4 spaces).

10. \v

This escape sequence prints a vertical tab (4 lines).

11. \0<nnn>

This escape sequence is a byte with the octal value of the specified value “nnn”.

12. \n<hh>

This escape sequence is a byte with the hexadecimal value specified by the user “hh.”

Now that we have laid down the fundamentals and theory of the echo command let us look at some examples of using the echo command.

Printing “Hello World!”

It would be a shame to print anything other than the cliched “Hello World” statement to print our first text using the echo command. To do so, write the echo command followed by the text you want to print such that it is enclosed in double-quotes (“”) as shown below:

printing hello world

Using escape sequences in linux

We saw that to use escae sequences. First, we must use the option “-e” and only then specify these sequences in our text. Let us look at some of these sequences in action.

1. Printing a new line

If you want to print a text in a new line, use the escape sequence “\n”. Use the syntax shown below:

echo -e “<text1> /n<text2>”

printing a new line

2. Printing a horizontal tab

If you want to print a horizontal tab (4 spaces), use the escape sequence “\t”. Use the syntax shown below:

echo -e “<text1> /t<text2>”

printing a horizontal tab

3. Printing a vertical tab

If you want to print a vertical tab (4 lines), use the escape sequence “\v”. Use the syntax shown below:

echo -e “<text1> /v<text2>”

printing a vertical tab

4. Printing a backslash

While using the echo command, If you want to print a backslash in your text, you cannot write the backlash as it is. Why? Because the echo command will think that you are trying to write an escape sequence and print an error. Therefore to print a literal backslash, you need to use the escape sequence “//” as shown below:

echo -e “<text1> //<text2>”

printing a backslash

5. Omitting part of a string in linux

While using the echo command, if you want to shorten part of the string by omitting the part that follows the escape character in the output, use “\c” as shown below:

echo -e “<text1> /c<text2>”

omitting part of a string

6. Changing the colour of a string

We can use the ANSI escape sequences to change the colour of the output text.

changing the colour of a string

Writing to a file in linux

If we want to write the text that we are printing with the echo command to a file, we can use the redirection operator (>>) as shown below:

sudo echo “<text>” >> <filename>

writing to a file

Printing variables in linux

We can also print variables using the echo command. For example, we can directly print the predefined environment variables as shown:

printing variables

Or we ourselves can assign a variable and then print it as shown below:

printing variables by assigning them

Displaying the output of other commands in linux

We can also print the output of other commands using the echo command, but why would you do that? Well, we can print a custom message before the command output, as shown below:

displaying the output of other commands

Here is another example:

example of displaying the output of other commands

Listing contents of the present directory using the echo command

We all know that we list the contents of a specific directory using the “ls” command. What if I tell you you could also do the same with the echo command? You can use the command “echo *” to print all the contents (directories and files) in the current directory.

listing contents of the present directory using the echo command

 

Similarly, you can also list only specific types of files by specifying the type after the wildcard (*) as shown below:

listing contents of the present directory using the echo command using the wildcard symbol

Combining escape sequences in linux

You can combine two or more escape sequences as you like. There is truly no limit! Let us look at one such example:

echo -e "\n\t<text1>\n\t<text2>"

Backspace in the echo command

If you want to use the backspace in your text in the echo command, use the escape sequence “/b” as shown below:

backspace in the echo command

Omit trailing new line

If you don’t want a new line then pair the echo command with the option “-n” as shown:

echo -n <text>

omit trailing new line

Printing the version of the echo command

If you want to print the information regarding the echo command you are using in your system, type the command “/bin/echo –version” in the terminal as shown below:

printing the version of the echo command

Printing the help menu of the echo command

If you want to print the help menu of the echo command, which contains the usage and information about the options and escape characters, type the command “/bin/echo –help” in the terminal as shown below:

printing the help menu of the echo command

Summary

As you have seen, the echo command is a simple tool to print text on the screen. You have now learned what echo is, why it is used, the syntax, options, and escape sequences of the echo command with a few examples.

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *