Unix Shell Scripts   «Prev  Next»
Lesson 5 A command to write to the screen
Objective Use the echo command to display data on the screen.

Command to Write to the Screen

Many scripts work in the background, copying files, handling configuration tasks, and doing other work that is not seen by the user. In other cases, a script must communicate with the user by writing information to the screen. The echo command is used to display a message onscreen. All of the text after the echo command itself is sent to the Standard Output channel (STDOUT), which normally appears on the screen. The echo command is used to give feedback, to inform users about error conditions, or simply to provide a message of the day. The echo command normally ends with a new line. If you use two echo commands in succession, each one will print to a separate line. For example, if these two commands are used in a script:
echo The sample configuration file cannot be found.
echo Please contact your vendor for information.

The result that appears on screen will look like this:
The sample configuration file cannot be found.
Please contact your vendor for information.

On some UNIX platforms the echo command can include the -n parameter to prevent the new line at the end of the text. For example, if these two commands are used in a script:
echo –n Contact your agent about the following file: 
echo /etc/smb.conf

The result that appears on screen will look like this:
Contact your agent about the following file:
/etc/smb.conf


This is often useful when the last part of a text line is calculated using an if/then decision statement.
The echo command prints everything on the rest of the line to STDOUT. It does not include quotation marks or worry about special characters. You can use an echo statement alone to print a blank line to the screen. This can be used to make other lines of text more readable. Now that you can write to the screen, learn to read from the keyboard in the next lesson.

SEMrush Software