13 Examples of Echo Command in Linux


The echo command is used to output the content of an argument(s) when they are passed to it. It is a built-in command and is available in every distribution. This command can be used in many combinations to perform various tasks.

In this post, I am going to demonstrate to you the usage of the echo command with examples on my Ubuntu 20.04 Linux distribution.

Prerequisites

There are no prerequisites for this command. All you need is a Linux machine.

Syntax

Below is the syntax of an echo command.

echo option [argument]

There are the following options also called switches for this command.

Options Description
\n Used to print text on a new line
-e Used for interpretation of options or backlash-escaped characters
\r Used for carriage return
\t Used for horizontal tab space
\v Used for vertical tab space
\b Used for backspace
\\ Used for backslash character
\a Used for sound or beep
-n Used for omitting the new line character

Examples

Displaying the text on a terminal

This is one of the basic examples (without any options used) of an echocommand which displays the text when enclosed in double quotes on your terminal.

echo “I am a man of my words”

Output:

You will see the following output.

how-to-use-cat-command-linux

Removing the space between words

If you want to remove spaces between words in the above sentence, run the following command with the -e option and press the enter key.

echo -e “\bI \bam \ba \bman \bof \bmy \bwords”

Output:

When the above command is run, it returns the output shown.

I-am-a-man-of-my-words-2

Displaying the text in double quotes

In this example, I want to display the text in double-quotes on a terminal. Therefore, I will be using backslash character \” as follows.

echo -e “\” I am a man of my words\” ”

Output:

You should see the following result.

how-use-cat-command-linux

Displaying the text in single quote

Similarly, if you want to display the text in single quote use the command as follows without any backslash character.

echo -e “I’m a man of my words”

Output:

You should see the following output on your terminal screen.

how-use-cat-command-linux

Displaying the text on multiple lines

As you can see from the above two examples, the command output the text on a single line. How about, if you want the text to be output or displayed on multiple lines.

Use the \n switch as follows:

echo -e “I am \na man \nof my words\n”

Output:

You should see the following output on your screen.

how-to-use-cat-command-linux

Displaying the text with horizontal tab spaces between words

If you want a tab space (9 characters) between words in your text, use the \t option as follows.

echo -e “I\t \tam \ta \tman \tof \tmy \twords”

Output:

You should see the output similar to the following screenshot.

how-to-use-cat-command-linux

Displaying the text with vertical tab spaces between words

In the above example, if you notice there is a horizontal tab spacing between words. If you like vertical tab spacing between words use the “\v” option.

echo -e “\vI \vam \va \vman \vof \vmy \vwords”

Output:

You should see the following output on your terminal.

how-to-use-cat-command-linux

Generating beep with a text

If you like a sound produced before a text is displayed on your terminal, use the \b option.

echo -e “\a I am a man of my words”

Output:

You should have the following output.

how-to-use-cat-command-linux

Displaying the text in colors

By default, the output of the echo command is displayed in black and white. If you want to display the text in different colors, you can do so with the help of ANSI escape code.
I want to display the string “I am a man of my words” in red color.

echo -e “\033[;31m I am a man of my words"

31 is the color code of red.

Output:

how-to-use-cat-command-linux

Similarly, if you want to print the text in yellow color and bold it. You have to alter the above command as follows.

echo -e “\033[1;33m I am a man of my words"

1 is used for bold.

Output:

You can see the text in yellow and bold color.

how-to-use-cat-command-linux

Displaying all files and directories

You can use the echo command to display all files and directories of your current working directory as follows. It works just like the ls command.

echo *

Output:

Below is the output when the above command is run on your terminal.

how-use-cat-command-linux

Displaying the output of a variable

You can also use the echo command to display the content of a variable. Suppose you want to assign value 10 to a variable x.

x=10

When a 10 has been assigned to the variable x, you can display it in the following fashion.

echo "value of x is : $x"

Output:

You should see the following output on your screen.

how-use-cat-command-linux

Output the content to a file

Instead of displaying text on the terminal screen, you can output it to a file using > character also called redirection or piping.

echo “I’m a man of my words” > test_file.txt

Output:

You should see the following output.

how-use-cat-command-linux

Conclusion

Thank you for reading this post. Have a good day!

Print Friendly, PDF & Email
Categories

1 comment

Add yours

Comments are closed.