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 as follows.

[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

' ' &ndash backspace with backslash interpretor ' -e' is used to remove all the spaces in between.

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

To create a new line

The option ‘ ‘ &ndash New line with backspace interpreter ‘ -e‘ treats new line from where it is used.

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


To have horizontal tab spaces

Option ‘ ‘ &ndash horizontal tab with backspace interpreter ‘ -e‘ is sued to have horizontal tab spaces.

[root@linuxhelp Desktop]# echo -e " Linuxhelp 	is 	a 	master 	in 	linux 	and 	Opensource" 
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 ‘ ‘ and horizontal tab ‘ ‘ simultaneously

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

Linuxhelp
is
a
master
in
linux
and
Opensource

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

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

Linuxhelp

is

a

master

in

linux

and

Opensource

To have specified carriage return in output

Option ‘ ‘ &ndash carriage return with backspace interpreter ‘ -e‘ is used to have specified carriage return in output.

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

To continue without emitting new line

Option ‘ c‘ &ndash 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‘ &ndash 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

You can print all the jpeg file by using the following command.

[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

Tag : Echo
FAQ
Q
How to add to last line in echo command
A
echo "hello world" >> file.txt
Q
I print the message using echo command. But, is it possible to view it in green color
A
echo -e "\033[32m hello world"
Q
How to check for a Specific environment variable in linux
A
echo "$variable_Name"
Q
How to use echo command to read and print content of a file
A
echo "$(
Q
How to delete content of a file using echo command
A
echo "" > file_name