Thursday, April 9, 2009

Displaying text with echo command

With echo command on unix you can display text to console.
The syntax is to use echo command is,

echo [options] ... [strings] ...

The strings contains the text to display and it would be within double quotes.
With options you can control how output will be displayed on console. In option section you can provide -n or -e or -E option.

If you use -n option, then after displaying strings new line will not be printed. By default if you use echo without -n options then after displaying strings trailing newline is printed.

If you use -e option, then while displaying strings any backslash character is interpreted as escape character. By default if you use echo without -e option backslash is printed if it is within string.

If you use -E option, then backslash character is not termed as escape character which is the default behavior of echo.

As with -e option of echo backslash (\) is interpreted as escape character so as -e is in effect, the following sequences can be used to print special output.


\0NNN the character whose ASCII code is NNN (octal)
\\ backslash
\a alert (BEL)
\b backspace
\c suppress trailing newline
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab

Below is an script that shows an example of echo.

Tahaa2:/home/arju/test# vi test_echo.bash
echo "This is normal echo without option"
echo "Bacslah (\) will be printed"
echo -n "New line will not be printed after end of line:"
echo "5"
echo -e "Special \n t\te\tx\tt"

Tahaa2:/home/arju/test# chmod +x test_echo.bash

Tahaa2:/home/arju/test# ./test_echo.bash
This is normal echo without option
Bacslah (\) will be printed
New line will not be printed after end of line:5
Special
t e x t

Related Documents
http://arjudba.blogspot.com/2009/04/system-notes-org-how-to-copy-and-paste.html
http://arjudba.blogspot.com/2009/04/what-is-kernel-shell-shell-script.html
http://arjudba.blogspot.com/2009/04/basic-steps-to-write-shell-script.html

No comments:

Post a Comment