Commands

echo Command in Linux: 7 Practical Examples

Learn to use the echo command in Linux with these simple but useful examples. The echo command is useful for displaying information in bash shell scripts.

Abhishek Prakash
Abhishek Prakash

Table of Contents

The echo command is perhaps one of the first few commands you see when you start learning Linux commands or bash shell scripting.

Echo is a simple command that simply prints its arguments on the display.

[email protected]:~$ echo Hello World
Hello World

You can guess why this command is called ‘echo’. Like the sound echo, the command also simply prints what it receives in the input.

Examples of the echo command in Linux

Echo is often used in shell scripts to display information such as asking for user to provide an input or displaying the error or warning for a certain action in the script.

The echo command has a simple syntax with a few options:

echo [options] [input string]

Let’s see some examples of the echo command.

1. Displaying value of a variable

Let’s say you declared a variable var = 100. You can display the value of this variable using echo command:

echo The value of variable var is $var

It’s output would be displayed as this:

The value of variable var is 100

2. Don’t print the final new line character

By default, the output of the echo command adds a new line to the output so that you have the output displayed in one entire line.

[email protected]:~$ echo Hello World
Hello World

You can suppress this new line character using the option -n:

[email protected]:~$ echo -n Hello World
Hello [email protected]:~$

3. Redirect the output to a file

You can use echo command to create a new file in Linux or append new text to an existing file by redirecting the output to a file:

echo "This text goes to a file" >> file.txt

3. Using escape characters with echo command

The option -e with echo allows you to interpret the escape characters. With escape characters, you can display your text on the screen in a different manner of your choice.

The following escape characters are available with escape:

  • \a – alert (plays a beep sound)
  • \b – backspace
  • \c – don’t print the final newline (same as -n option)
  • \f – from feed
  • \n – newline
  • \r – carriage return
  • \t – horizontal tab
  • \v – vertical tab
  • \\ – backslash
  • \’ – single quote
  • \” – double quote

While -e option asks the echo command to interpret the escape characters, the -E option does the opposite and ignores the escape characters.

Now let’s see how to use some of these escape characters with echo command.

echo -e "An eye for an eye leaves the whole world blind\n\t- Gandhi"

Can you guess what would be the output of the above command? Here it is:

An eye for an eye leaves the whole world blind
– Gandhi

Let’s see some other examples of escape characters. The carriage return character prints only the text after that character.

[email protected]:~$ echo -e “Hello\rWorld”
World

If you use the backspace character, it will remove one character before it:

[email protected]:~$ echo -e “Hello \bWorld”
HelloWorld

You can play with other escape characters in the similar way.

4. Display text with single or double quotes with echo command

Dealing with single quotes and double quotes is often a headache.

If you have a single quote in the text that you want to display, you should wrap the entire input within double quotes:

[email protected]:~$ echo “It’s My World”
It’s My World

If you want to display the double quotes, you have to ‘escape’ it:

[email protected]:~$ echo “It’s \”My\” World”
It’s “My” World

5. Display files in directory

You probably use the ls command to display the files in a directory. You can also use echo command for this purpose.

To display the contents of the present directory, you can use this command:

echo *

You can also use echo command to display only a certain type of files:

echo *.txt

If you want to list only the directories, use it like this:

echo */

6. Use echo command to empty a file in Linux

I have already shown this tip for emptying log files in Linux. If you have a text file and you want to clear its content without deleting the file itself, you can use the echo command in the following fashion:

echo > filename

Bonus Tip: Display echo command output in color

You probably would have come across some bash script that displays the text output in different color. Have you ever wondered how it is done? Let me show you that.

You can use the ANSI escape sequence and change the appearance of the output like background color, underscore, bold etc.

To change the color of the output text, you can use these:

echo -e “\033[30mThis is Black”
echo -e “\033[31mThis is Red”
echo -e “\033[32mThis is Green”
echo -e “\033[33mThis is Yellow”
echo -e “\033[34mThis is Blue”
echo -e “\033[35mThis is Magenta”
echo -e “\033[36mThis is Cyan”
echo -e “\033[37mThis is White”

Sample colored output for your understanding:

Echo Command Color
Echo command output in color

\033[ represents ESC[ in the above commands.

Well, that’s it. I think you have seen a good number of examples of the echo command in Linux. If you use echo for some specific purpose that could be useful to others, please share it with us in the comment section.



Join the conversation.