1. Overview

In Linux, echo is one of the most basic and widely used commands. In this tutorial, we’ll learn the echo command and its options through examples.

2. Which echo Command?

Most modern Linux distros take bash as their default shell. And most shells implement echo as a built-in command — including bash, zsh, csh, and ksh. Therefore, when we are in bash, for example, the echo command is available as a shell built-in.

In this article, we’ll focus on the bash built-in echo command.

There is another echo command from the GNU coreutils package, which is also available in all Linux distributions.

When we are working in a terminal, in most cases, we’ll use the builtin echo.

To see the two echo commands, we can use another built-in command: type with the -a option:

$ type -a echo
echo is a shell builtin
echo is /usr/bin/echo

3. Introduction to the echo Command

The echo command writes text to standard output (stdout).

The syntax of using the echo command is pretty straightforward:

echo [OPTIONS] STRING...

For example:

$ echo "Hello World from the echo command"         
Hello World from the echo command

Some common usages of the echo command are piping shell variable to other commands, writing text to stdout in a shell script, and redirecting text to a file.

We’ll see some more examples of the echo command in later sections.

4. Print Variable Values

An everyday use of echo is to display the value of an environment variable:

$ echo $JAVA_HOME
/usr/lib/jvm/default

If we wrap shell variables in double quotes, the echo command will print values of the variables. If we don’t want a variable to be converted to its value, we can escape the dollar ($) sign:

$ echo "\$JAVA_HOME : $JAVA_HOME"
$JAVA_HOME : /usr/lib/jvm/default

As an alternative to escaping the dollar sign, we can wrap the string in single quotes. This is because shell variables in single quotes will not be expanded:

$ echo '$JAVA_HOME :' $JAVA_HOME  
$JAVA_HOME : /usr/lib/jvm/default

5. Print the Output of a Command

If we put a command substitution inside double quotes, the echo command will print the output of the command execution:

$ echo "My Linux booted at $(uptime -s)"
My Linux booted at 2020-02-14 10:41:50

6. Omit the Trailing Newline

By default, the echo command will output a newline character at the end of the string.  We can tell echo to omit the trailing newline using the -n option:

[email protected]:/tmp$ echo -n "Some text without trailing newline*"
Some text without trailing newline*[email protected]:/tmp$

As we can see in the output above, the prompt comes immediately after the output of the echo command due to the absence of the trailing newline.

It’s also pretty handy to empty a file without deleting it using echo and this -n option:

$ echo -n "" > /tmp/boot_history.txt
$ file /tmp/boot_history.txt
/tmp/boot_history.txt: empty

7. Print Backslash-Escape Characters

First, let’s have a look at another example:

$ echo "Line 1\nLine 2\nLine 3\nvtab1\vvtab2\vvtab3"
Line 1\nLine 2\nLine 3\nvtab1\vvtab2\vvtab3

In the text above, there are backslash-escaped characters. The echo command will print them literally by default.

If we pass the -e option to the echo command, it will interpret the following backslash-escaped characters:

  • \\ – displays a backslash character
  • \a – alert (BEL)
  • \b – displays a backspace character
  • \c – suppress any further output
  • \e – displays an escape character
  • \f – displays a form feed character
  • \n – displays a newline
  • \r – displays a carriage return
  • \t – displays a horizontal tab
  • \v – displays a vertical tab

Now, let’s pass the -e option to echo, see what it will give us:

$ echo -e "Line 1\nLine 2\nLine 3\nvtab1\vvtab2\vvtab3"
Line 1
Line 2
Line 3
vtab1
     vtab2
          vtab3

Except for outputting those backslash-escaped characters, we can let echo command output text in colors by passing ANSI escape sequences to it:

$ echo -e "\033[0;32mINFO In Green \033[0m"
$ echo -e "\033[0;33mWARN In Yellow \033[0m"
$ echo -e "\033[0;31mERROR In Red \033[0m"

We’ll see the following output if we execute the commands above:

echo command output text in color

8. Conclusion

The echo command is a simple one, but we use it very often.

In this article, we’ve learned some typical usages of the echo command through examples. By now, we can control the output using various options.

We know how to get a colorful output using the echo command as well.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments