Previous Table of Contents Next

Module 38


The internal echo command displays its arguments to your terminal. More precisely, echo writes its arguments on the standard output. The arguments are strings separated by blanks and terminated by a new-line. echo understands the more popular C-like escape character sequences. Some of the features and uses of echo are:

*  Display strings of text.
*  Display variables.
*  Display output with various formatting characters (SV only).
*  Display ASCII characters based on octal escape sequences (SV only).
*  Display menu screens for shell scripts.
*  Display prompts for input to shell scripts.


Following is the general format of the echo command.

     echo [ -n ] [ arg ... ]


The following list describes the options and their arguments that may be used to control how echo functions.

-n Causes echo not to output a new-line character. Thus the text is displayed and the cursor remains after the last displayed character. For example:
                      echo -n "Enter choice: "
                      Enter choice: _
                      echo "Enter choice: "
                      Enter choice:
Basically you use the -n for shell script prompt lines.

The -n is also supported by the ksh print command. So if you have echo as an alias for print - the -n option will work just like on BSD and csh echo.


The arguments to echo are whatever text you wish to write to the standard output. If no arguments are specified, then echo displays a blank line by writing a single new-line character. You can use the following list of escape sequence characters to produce special output from echo:

You must remember that the shell interprets the (backslash) as a special character. Therefore, it is recommended that you place your echo arguments inside double quotes.

\b produces a backspace on the output.
\c writes the output, but does NOT terminate it with a new-line. Must be last.
\f produces a form feed.
\n produces a new-line.
\r produces a carriage return.
\t produces a tab.
\v produces a vertical tab.
\\ produces a backslash.
\0n produces the ASCII character with the octal code of n, where n is a 1-, 2-, or 3-digit octal number representing the desired character.

BSD (Berkeley)
The BSD and csh versions of echo do not support the escape sequences listed above.


The following examples show some of the useful aspects of this simple command:

     cj echo *              # display all files in current directory
     cj echo *.[ch]         # display all files ending with a .c or .h
     cj echo $TERM          # display the value of the TERM variable


When you use the \0n escape sequence you must always precede the n with a 0 (zero). This informs echo that the number is octal.

To use escape sequences you must either enclose them in double quotes, single quotes, or use two \ (backslashes).

If you need to use these escape characters, the octal equivalents can be found in Appendix G.


Refer to the print command described in Module 106 and the ksh command described in Module 71.


The echo command writes to the standard output. Therefore, you may redirect it to a file or a pipe.


The echo command is used to display text and variables to the user's terminal. Since the standard output is the default, output may be redirected as desired. It is often used to direct known output into a pipe. It is used in shell scripts to display return codes and debugging output. Menus and data entry screens can be built using echo. The select command should also be studied for menu building.


In this activity you use the echo command to display text and variables. You also use it to output new-lines, tabs, and no new-line via escape sequences. Begin at the shell prompt.

C Shell
If you are using the csh, type sh and press Return to enter a Bourne shell for the following examples. Eliminate the "--" and escape sequences from the echo commands if present. Replace the "\t" notation with tabs.

1.  Type echo "My home directory is $HOME" and press Return.
2.  Type echo "`tput clear`\n\n\t\t\t$LOGNAME Command Menu" and press Return. Your screen should resemble the following:
     cj echo "`tput clear`\n\n\t\t\t${LOGNAME} Command Menu"

The screen clears and the line is displayed:

              mylogin Command Menu

C Shell
Type echo "`clear`^V^M^V^M^I^I^I$USER Command Menu" and press Return.

3.  Type echo "Press Return to continue: \c" and press Return. Notice the cursor remains at the end of the output string. Your cursor prompt is placed at the end of the string. The \c informs echo not to output a new-line. This is often used in shell scripts to create input prompts.

C Shell
Type echo -n "Press Return to continue:" and press Return. The -n prevents echo from writing a new-line so the cursor stays at the end of the line.

4.  Turn to Module 106 to continue the learning sequence.

Previous Table of Contents Next