Echo

Index

ECHO is primarily used in batch files both to display messages and to prevent the display of commands. It is also used to feed characters to other commands using redirection.

Syntax:

To display the current echo setting:
ECHO

To turn command echoing on or off:
ECHO [ON | OFF]

To display a message:
ECHO message

To force a blank line:
ECHO. or ECHO, or ECHO"

To sound a beep:
ECHO ^G (Ctrl+G)

Notes:

  1. By default, as a batch file runs, each command is displayed on the screen just as if it had been entered from the keyboard. The ECHO command is typically used to control this often unnecessary and distracting output.

  2. The maximum length of message is 122 characters. If message is too long to fit on one line, it will wrap - but not necessarily neatly! For multi-line messages it is better to use a series of ECHO commands with each message less than 75 characters. Actually, 30 to 40 characters probably makes for better readability. For long messages containing instructions perhaps, it may be preferable to prepare a separate text file which can be displayed using the TYPE command.

  3. If ECHO OFF is entered on the command line, not even the command prompt is displayed. However, commands can be entered and error/warning messages are displayed as normal. To redisplay the command prompt, type ECHO ON.

  4. In a batch file, prefixing a command with @ will stop that particular line being displayed.

  5. ECHO can be used with redirection to send a character string (message) to a command, file or device. This can be very handy in batch files using commands requiring predictable user input.

  6. To include a redirection character (<, >, >>, or pipe ( | )) in an ECHO message, it must be enclosed in inverted commas to prevent redirection (or attempted redirection) actually occurring.

Examples:

  1. The following batch program fragment includes a two-line message preceded by a blank line:
    ECHO off
    ECHO.
    ECHO This batch program formats new disks

    This would display as:

    ECHO off
     
     
    This batch program formats new disks

  2. In the above example, to stop the ECHO command being displayed, use the @ prefix. Thus:
    @ECHO off
    ECHO.
    ECHO This batch program formats new disks

    Would display as:

     
     
    This batch program formats new disks

  3. To automatically feed a "Y" in response to DEL's "are you sure" warning before deleting all files in a directory:
    ECHO y | DEL c:\data\*.*
    Note that the warning message is still displayed, but the response is supplied by the "ECHO y"

  4. Similarly, to display the date without having to press [Enter] in response to the prompt:
    ECHO. | DATE
    Again, the prompt is still displayed, but this time the response is supplied by the "ECHO."

  5. To display the instruction: Enter a number >10:
    ECHO "Enter a number >10"
    Omitting the inverted commas would cause a file called "10" to be created in the current directory containing the text "Enter a number".

Tricks

As mentioned in the "Notes" above, if you wish to include any of the redirection symbols (<, >, >>, or pipe ( | )) in message, they must be enclosed in inverted commas. These inverted commas are, of course, displayed in the ECHOed message. Tom Lavedas pointed out a way to avoid the quotation marks being displayed by following each mark with a backspace character (ASCII 8, Ctrl-H). To include such a character you have to be writing the batch file with a text editor such as EDIT that allows the entry of such "special" characters - in the case of EDIT, you would enter Ctrl-P, Ctrl-H.
For some reason, the backspace does not work if it is the last character on the line - it must be followed by something - even a space.
Tom Lavedas also pointed out that this technique could cause problems when printing out the batch file on some printers.

File Details:

Internal


This page last revised:
December 9, 1999.