Linux echo Command with Options & Examples
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.
The syntax for linux command
Display string with options
echo [option] [String]
[string] are known as arguments to the command
First, let's try
echo without any option to display some string.
Let's display string "Hello World!" using
$ 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
newprism.js are files in current directory or folders. This is same as Linux command
With echo command we can use
-E is default option in echo and it disables interpretation of backslash escapes. So, by default escape characters are not interpreted.
-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
\t are interpreted as newline and tab when using with
-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
For more detail, run
man echo on terminal to get complete information about