echo-command-in-linux-image-first

Top Examples of echo command in Linux Tutorial for Beginners

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.

[[email protected] ~]$ echo hello Friends, How are you?
hello Friends, How are you?
[[email protected] ~]$ echo hello friends, how are you doing?
hello friends, how are you doing?
[[email protected] ~]$ 

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 “”

[[email protected] ~]$ echo -e  "hi \bfriends \bhow \bare \byou?"
hifriendshowareyou?
[[email protected] ~]$ 

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:

[[email protected] ~]$ echo -e  "hi \nfriends \nhow \nare \nyou?"
hi 
friends 
how 
are 
you?
[[email protected] ~]$ 

Option 3: \r – carrige return

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

See example below:

[[email protected] ~]$ echo -e  "hi friends, \rhow are you?"
how are you?
[[email protected] ~]$ 

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:

[[email protected] ~]$ echo -e  "hi \tfriends \thow \tare \tyou?"
hi 	friends 	how 	are 	you?
[[email protected] ~]$ 

Option 5: \v – vertical tab

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

See example below:

[[email protected] ~]$ echo -e  "hi \vfriends \vhow \vare \vyou?"
hi 
   friends 
           how 
               are 
                   you?
[[email protected] ~]$ 

Special Offer on Ethical hacking Course in Hindi

Ethical-hacking-course-in-hindi-50-off

If Appreciate My Work, You should consider: