Linux echo Command with Options & Examples

In Linux, echo is built-in command which is used to display some string or text. String or text is passed as an argument.

This command runs on different distributions of Linux based operating systems like Ubuntu, Fedora, Debian, Kali, Red Hat etc. In this tutorial we will discuss syntax, available options for command echo with examples.

echo Syntax

The syntax for linux command echo is:

Display string


echo [string]

Display string with options


echo [option] [String]

Here, [option] and [string] are known as arguments to the command echo.

echo Examples

First, let's try echo without any option to display some string.

Let's display string "Hello World!" using echo command:


$ echo Hello World!
Hello World!

$ echo "Hello World!"
Hello World!

You can pass string or text as an argument to the echo with or without double quotes i.e. "".

Displaying value of variable using echo


$ x=100
$ echo "Value of x = $x."
Value of x = 100.

In the above example, we first set value of variable x=100 then displayed its value using echo "Value of x = $x". Here $x is placeholder for value of x.

Displaying all files and folders in current directory using echo


$ echo *
linux.jpg newprism.js

Note: here linux.jpg and newprism.js are files in current directory or folders. This is same as Linux command ls.

echo Options

With echo command we can use -e or -n or -E options.

-E is default option in echo and it disables interpretation of backslash escapes. So, by default escape characters are not interpreted.

-e Option

Option -e is used to enable interpretation of backslash escape characters.

echo -e Examples


------------------
Without -e options
------------------

$ echo "Hello\nThere!"
Hello\nThere!   

$ echo "Welcome to \tCodesansar."
Welcome to \tCodesansar.


------------------
With -e options
------------------

$ echo -e  "Hello\nThere!"
Hello
There!


$ echo -e "Welcome to \tCodesansar."
Welcome to      Codesansar.

In the above example \n and \t are interpreted as newline and tab when using with -e option.

-n Option

Option -n is used to tell control that do not print the trailing newline after completion of command.

echo -n Examples


$ echo "Without -n option"
Without -n option

$ echo -n "With -n option"
With -n option$ 

In the above example, check the position of $ sign with and without option -n.

For more detail, run man echo on terminal to get complete information about echo command.