linux beginner
Now Reading
Echo Command in Linux with Examples
0

Echo Command in Linux with Examples

Echo Command in Linux

Echo is a built in command for bash and C shells. It can be used to display a line of text on a standard output or it could able to append into a new file.

Syntax

echo <options> <contents>

To input a line of text and display on standard output

[root@linuxhelp Desktop]# echo Linuxhelp is a master in linux and Opensource
Linuxhelp is a master in linux and Opensource

To declare a variable and echo its value

We will declare a variable of x and assign its value to 10


[root@linuxhelp Desktop]# x=100

Echo its value:


[root@linuxhelp Desktop]# echo The value of varible x = $x
The value of varible x = 100

Note: -e in Linux acts as interpretation of escaped characters that are back slashed.

To remove all the spaces in between

\b‘ – backspace with backslash interpretor ‘-e‘ is used to remove all the spaces in between.


[root@linuxhelp Desktop]# echo -e "Linuxhelp \bis \ba \bmaster \bin \blinux \band \bOpensource"
LinuxhelpisamasterinlinuxandOpensource

To create a new line

The option ‘\n‘ – New line with backspace interpreter ‘-e‘ treats new line from where it is used.


[root@linuxhelp Desktop]# echo -e "Linuxhelp \nis \na \nmaster \nin \nlinux \nand \nOpensource"
Linuxhelp
is
a
master
in
linux
and
Opensource

To have horizontal tab spaces

Option ‘\t‘ – horizontal tab with backspace interpreter ‘-e‘ is sued to have horizontal tab spaces.


[root@linuxhelp Desktop]# echo -e "Linuxhelp \tis \ta \tmaster \tin \tlinux \tand \tOpensource"
Linuxhelp is a master in linux and Opensource

To have vertical tab spaces


[root@linuxhelp Desktop]# echo -e "\vLinuxhelp \vis \va \vmaster \vin \vlinux \vand \vOpensource"

Linuxhelp
is
a
master
in
linux
and
Opensource

To use option new Line ‘\n‘ and horizontal tab ‘\t‘ simultaneously


[root@linuxhelp Desktop]# echo -e "\n\tLinuxhelp \n\tis \n\ta \n\tmaster \n\tin \n\tlinux \n\tand \n\tOpensource"

Linuxhelp
is
a
master
in
linux
and
Opensource

To use option new Line ‘\n‘ and vertical tab ‘\v‘ simultaneously


[root@linuxhelp Desktop]# echo -e "\n\vLinuxhelp \n\vis \n\va \n\vmaster \n\vin \n\vlinux \n\vand \n\vOpensource"

Linuxhelp

is

a

master

in

linux

and

Opensource

To have specified carriage return in output

Option ‘\r‘ – carriage return with backspace interpreter ‘-e‘ is used to have specified carriage return in output.


[root@linuxhelp Desktop]# echo -e "Linuxhelp \ris a master in linux and Opensource"
is a master in linux and Opensource

To continue without emitting new line

Option ‘\c‘ – suppress trailing new line with backspace interpreter ‘-e‘ is used to continue without emitting new line.


[root@linuxhelp Desktop]# echo -e "Linuxhelp is a master \cin linux and Opensource"
Linuxhelp is a master [root@linuxhelp Desktop]#

To omit echoing trailing new line

-n option is used to omit echoing trailing new line.


[root@linuxhelp Desktop]# echo -n "Linuxhelp is a master in linux and Opensource"
Linuxhelp is a master in linux and Opensource[root@linuxhelp Desktop]#

To have sound alert

Option ‘\a‘ – alert return with backspace interpreter ‘-e‘ is used to have sound alert.


[root@linuxhelp Desktop]# echo -e "Linuxhelp is a master in linux \aand Opensource"
Linuxhelp is a master in linux and Opensource

To print all the files/folder using echo command


[root@linuxhelp Desktop]# echo *
111.txt file1.txt file2.txt untitled folder

To print files of a specific kind

To print all ‘.jpeg‘ files


[root@linuxhelp Desktop]# echo *.jpeg
a1.jpeg a2.jpeg a3.jpeg abc.jpeg b1.jpeg b2.jpeg c1.jpeg

To output to a file and not standard output

The redirect operator can be used with echo to output to a file and not standard output.


[root@linuxhelp Desktop]# echo "Linuxhelp Page" > linuxhelppage
[root@linuxhelp Desktop]# cat linuxhelppage
Linuxhelp Page

Echo Options

Options Description
-n do not print the trailing newline.
-e enable interpretation of backslash escapes.
\b backspace
\\ backslash
\n new line
\r carriage return
\t horizontal tab
\v vertical tab

Thank you! for using Linux Help.

You find this tutorial helpful? Share with your friends to keep it alive.

For more help topics browse our website www.linuxhelp.com

Be the first to comment, we value your suggestions. For further queries please comment below.

Leave a Response