Linux Echo Command and Usage Examples

Echo is popular command comes with all of the Linux distributions. Echo is provided by bash and C shells. Echo simple outputs are given values to the console or terminal.

Man Echo

To get more detailed help and echo official documentation use following command

$ man echo
Man Echo
Man Echo

Syntax

Echo command provides two types of syntax like below.

echo [SHORT-OPTION]... [STRING]...

OR

echo LONG-OPTION

Display Text

Simple and most common usage of echo command is just printing specified text to the standard output like below.

$ echo Poftut is very useful
Display Text
Display Text

Print Variables

Another popular usage form is printing shell variables like below.

$ echo My terminal is $TERM
Print Variables
Print Variables

Remove Spaces While Printing

There is -e option which will remove all spaces by using backspaces defined with \b while printing text

$ echo -e "This \bis \bpoftut"
Remove Spaces While Printing
Remove Spaces While Printing

Bash Echo New Line

Another useful usage is adding \n control character which will print a new line.

$ echo -e "This \nis \npoftut"
New Line
New Line

Tab

While printing some text to the standard output tabs can be placed into this text by using \t

$ echo -e "This \tis \tpoftut"
Tab
Tab

Vertical Tab

Vertical tab will provide new line and align the output text like below.

$ echo -e "This \vis \vpoftut"
Vertical Tab
Vertical Tab

Carriage Return

A carriage return will delete previous part of the text and only print after last carriage return with \r

$ echo -e "This \ris \rpoftut"
Carriage Return
Carriage Return

Backspace

Backspace control character will delete a single character to the backward with \b in the text.

$ echo -e "This \bis \bpoftut"
Backspace
Backspace

Omit New Line

Normally after printing text to the standard output new line is provided to make things more clear. But this new line can be prevented with option -n 

$ echo -e -n "This is poftut"
Omit New Line
Omit New Line

Alert

Alert can provided to the shell subsystem with \a . This is very useful to make shell scripts and application more interactive

$ echo -e "This is poftut\a"
Alert
Alert

This option will not affect the output of the text.

Print All Files and Folders

Echo command can work with bash features easily. To easily and simply print all files and folders of the current directory following command can be used.

$ echo *
Print All Files and Folders
Print All Files and Folders

Print specific Extension Files

While printing files and folders specific extensions can be used to filter output. For example, we want to print to the standard output only c source files with the extension .c 

$ echo *.c
Print specific Extension Files
Print specific Extension Files

Write Into File

Without opening any text editor some text can be written to a file. The file must not preexist. If file is all ready exist all content is deleted and given text is written to the text file.

$ echo "This is poftut" > mystory.txt
Write Into File
Write Into File

Create Empty File

An empty file can be created by providing an empty string definition and redirecting this to a file.

$ echo "" > empty_file.txt
Create Empty File
Create Empty File

Add To The File

The last example of the echo command is adding more text to the existing file. Adding operation will no delete existing text in the file.

$ echo "This is poftut" >> mystory.txt
Add To The File
Add To The File

Leave a Comment