Saturday, 28 January 2012



echo is used to display characters into standard output. It can also be used to write to a file
or  just to print a newline character.

It can be used to print a string and a variable together.

/home/b4578$ echo  “ my Home is $HOME”
My Home is /home/b4578

To print the $symbol use a backslash “\” character.
/home/b4578$ echo  “my \$HOME variable is set to $HOME”
my \$HOME variable is set to /home/b4578

Example 2) 
To display double quote character “ use a backslash.
Suppose you want to display   a  html form tag. use echo as shown in a script.
#..........lines of code
echo “<html>”
echo “<body>”
echo “<form action = \”stratnew.js\” method = \”post\”>
echo “<input type = \”text\” name = \”hname\” >
.......... # lines of  html form
echo “</form>”
echo “</body>”
echo “</html>”

when you run the script the output will be
echo “<html>”
echo “<body>”
echo “<form action = ”stratnew.js” method = ”post”>
echo “<input type = ”text” name = ”hname” >
.......... # lines of  html form
echo “</form>”
echo “</body>”
echo “</html>”

Example 3)
to just display a newline in the output of a script, simply place an  echo.

Example 4)
 To write the output of echo command to a file use “>” to redirect the output of echo.
/home/b4578$echo  “ my logpath is $LOGPATH”  >>file_log.txt
/home/b4578$tail   -1  file_log.txt
my logpath is  /home/b4578/user/logs
Example 5)
To give an audible alert(a bell) use \a inside echo
For example to make an alarm script which beeps continuously when there is any error, the echo command can be used as shown.
/home/b4578$vi   check
alarmfunc( )
While  [ 1 ]           # always condition
echo  “\a There is some problem”        #beep continuously with a gap of one second.
sleep 1
If  [  $tran_time_before   -gt   20 ]

Escape sequences  which are recognised by echo are listed below.

\a    Displays an alert character.

\b   Displays a backspace character.

\c    Suppresses the new-line character that otherwise follows the final argument in the output.   
         All characters  following the \c sequence are ignored.
\f   Displays a form-feed character.
\n   Displays a new-line character.
\r    Displays a carriage return character.
\t    Displays a tab character.
\v    Displays a vertical tab character.
\\    Displays a backslash character.

No comments:

Post a Comment