Skip to main content
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

Warp Terminal

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

abhishek@linuxhandbook:~$ 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 the user to provide 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 the value of a variable

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

echo The value of variable var is $var

Its output would be displayed as this:

The value of variable var is 100

2. Don’t print the final newline 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.

abhishek@linuxhandbook:~$ echo Hello World
Hello World

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

abhishek@linuxhandbook:~$ echo -n Hello World
Hello Worldabhishek@linuxhandbook:~$

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

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

abhishek@linuxhandbook:~$ echo -e “Hello\rWorld”
World

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

abhishek@linuxhandbook:~$ echo -e “Hello \bWorld”
HelloWorld

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

5. 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:

abhishek@linuxhandbook:~$ echo “It’s My World”
It’s My World

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

abhishek@linuxhandbook:~$ echo “It’s \”My\” World”
It’s “My” World

6. 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 */

You cannot view the file content with echo, unfortunately.

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

Here are a few bonus tip to use the echo command in unusual or colorful manner.

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 quickly show you how to change the color of echo output.

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 output in color

\033[ represents ESC[ in the above commands.

See what a command will do without running it

In many cases, the echo command saves you the trouble of accidentally doing something you didn't expect.

It allows you to expand the wildcards to understand what will happen before you actually run the command.

echo rm -R *

This will output what will be passed to the rm command (and therefore what would be deleted), putting echo before a command renders it harmless (it just expands wildcards so you know what it will do).

What else?

The echo command is good for quickly printing desired output on the screen. If you want a more complicated formatted output, use the C-styled printf command in bash.

Bash printf Command Examples [Better Than Echo]
You may print simple outputs with echo command but that’s not enough for complicated formatted outputs.

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.

Abhishek Prakash