Exploring the Power of echo Command in Linux: Top Examples for Beginners

echo-command-in-linux-image-first

echo command in Linux is mostly used in bash/shell scripting. It is not only useful for programmers but useful for other Linux user as well. echo command in Linux is used to display a line of text/string that is passed as an argument.

You can use this command to print the value of a variable on the screen.

The basic syntax of echo command in Linux

Every command has basic syntax, of course I will share basic syntax of echo command in Linux. The syntax is:

echo [option] [string]

You must read the manual page to know more about this command.

Use the echo command to display a text/string value

You can use echo command to display a text/string value on the screen.

The string is a sequence of characters (alphabets), it is not a numeric value.

$echo “text/string value here”

Replace “text/string blue here” by content you want to display on the screen.

[vijay@localhost ~]$ echo hello Friends, How are you?
hello Friends, How are you?
[vijay@localhost ~]$ echo hello friends, how are you doing?
hello friends, how are you doing?
[vijay@localhost ~]$ 

Options used with echo command in Linux

There are multiple options available with echo command, similar to other utilities in Linux.

To know more about options you can use the following commands:

$man echo

OptionsDescription
-n  do not print the trailing newline
-e  enable interpretation of backslash escapes.
\bbackspace
\\backslash
\nnew line
\rCarriage Return
\thorizontal tab
\vvertical tab

Option 1: \b – backspace

Keep in mind before using ‘\b – backspace’ it is always used with option -e. This option removes all the space between two strings.

For example my string “Hi friends, how are you?” multiple spaces between the words. And I want to remove them by using the single option, then it is ‘\b’

Note: You must write your string between double quats “”

[vijay@localhost ~]$ echo -e  "hi \bfriends \bhow \bare \byou?"
hifriendshowareyou?
[vijay@localhost ~]$ 

Option 2: \n – newline

this interpreter is used \n – newline new line. It prints the next words into the new line where it is used.

For example, My string is “hell Friends”, I want to print friends into a new line so I will use \n option before Friends.

See the Result below:

[vijay@localhost ~]$ echo -e  "hi \nfriends \nhow \nare \nyou?"
hi 
friends 
how 
are 
you?
[vijay@localhost ~]$ 

Option 3: \r – carrige return

It is used for carriage output from the string, you must use with enterpreter -e.

See example below:

[vijay@localhost ~]$ echo -e  "hi friends, \rhow are you?"
how are you?
[vijay@localhost ~]$ 

Option 4: \t – Horizontal Tab

It is used with option -e interpreter and for horizontal tab space.

For Example, My string is “Hello Friends” and I want to print friends with horizontal tab space the I will use the string “Hello \tFriends”

See the result below:

[vijay@localhost ~]$ echo -e  "hi \tfriends \thow \tare \tyou?"
hi 	friends 	how 	are 	you?
[vijay@localhost ~]$ 

Option 5: \v – vertical tab

vertical tab is used with backspace interpreter ‘-e‘ to have vertical tab spaces between string values.

See example below:

[vijay@localhost ~]$ echo -e  "hi \vfriends \vhow \vare \vyou?"
hi 
   friends 
           how 
               are 
                   you?
[vijay@localhost ~]$ 

If you like our content, please consider buying us a coffee.
Thank you for your support!