07 October 2011

echo command in Linux/Unix

Leave a Comment
echo command in Linux/Unix

echo command in Linux/UNIX is primarily used to :

 1. To display message ( it is used in shell scripts).
 2. To evaluate shell variables (like echo $PATH)


1. echo "my name is xyz"     -- double quotes is not mandatory

output : 
$ echo "my name is xyz"
my name is xyz

$ echo my name is sandeep
my name is sandeep

2. echo $SHELL


$echo $SHELL
--this output can be different depending upon the shell you are using.

echo can also be combined with different commands like

1. echo " today date is `date`"

$ echo "today date is `date`"
today date is Sun Jan 8 13:16:04 IST 2012

echo used different escape sequence which are listed below :

* note that all escape sequence in Linux is use with "-e" option other wise it is not recognized by Linux

1. /b  - use for backspace

a. echo " my name is\b xyz"  --- used without -e option

 output : 
$echo "my name is/b xyz"
my name is/b xyz

b. echo -e " my name is\b xyz"    --used with -e option

 output : 
$ echo -e "my name is\b xyx"
my name i xyx

2.  /c - No new line (cursor appear in same line)

 echo -e " my name is xyz \c"

$ echo -e "my name is xyz\c"
my name is xyz$

* note that prompt appear in same line along with the output.

3. \n - Newline(same as enter)

echo -e " my name is xyz \n"

$ echo -e "my name is xyz\n"
my name is xyz


4 . \t - Tab

 echo -e " my\tname\tis\txyz"

$ echo -e "my\tname\tis\txyz"
my name is xyz

5 .  \f - formfeed

 echo -e " my\fname\fis\fxyz"

output :
$ echo -e "my name\fis\fxyz "
my name

other options  are :

 \a - bell
 \r - carriage return
 \\  - backslash

If You Liked This Post Please Take a Time To Share This Post

You May Also Like...


Post a Comment