Batch Programming ECHO Command

How To Display Messages and Turn Echoing On or Off in Command Line

© Guy Lecky-Thompson

A quick guide to using the command line batch programming echo keyword when creating Windows and MS-DOS batch files.

Batch file programming is a very useful way to automate small, repetitive, tasks. For those who are new to creating batch files, the Windows Command Line Programming tutorial is a good starting place to read about the basics of batch file programming.

The ECHO command can be used for 3 purposes:

One important point to remember is that ECHO relates only to the operating system standard output. For example, program output (which includes many of the standard command line utilities) is not affected.

This can be tested by entering the command line (Start->Run... then enter cmd and press enter, in Windows) and typing ECHO OFF(and then pressing enter). Keypresses remain echoed, as does program output (for example, the dir command still produces the desired effect). However system information, like the prompt, disappears. Luckily ECHO can be easily turned back on again.

Turning ECHO On and Off

Using the ECHO keyword with either ON or OFF turns screen ECHO on or off. In a batch file, if ECHO OFF is specified right at the start, then only command output, or messages displayed using the ECHO keyword will be output to the screen.

There is no need to end the batch file with ECHO ON, as the echo status will be restored once the batch script returns to the command prompt. Any time that ECHO is off, a message can be displayed by using the ECHO keyword.

Displaying Messages

The parameter given to the ECHO keyword represents the text to be echoed to the screen. So, the following will echo some text, and one of the parameters from the command line:

ECHO OFF
ECHO You gave the parameter %1 on the command line.

If the ECHO OFF command is not included, then each line from the batch script is displayed before it is executed - which is useful for debugging, but not necessarily for deployment. Without any parameters, the ECHO keyword will indicate the current status.

Displaying the ECHO Status

The final variation is the simplest : simply typing ECHO by itself, or using ECHO without parameters in a batch file, will display the current status, which will be one of:

ECHO is on.

or

ECHO is off.

It is interesting to note that this works even when ECHO is off! In addition, readers might be wondering how it is possible to display a blank line, given that ECHO by itself returns the ECHO status. The answer is simple:

ECHO.

The period (.) after the ECHO keyword is not echoed, but a new line is inserted. To echo the period, just introduce a space between the period and the ECHO keyword.

Other Batch File Commands


The copyright of the article Batch Programming ECHO Command in Command Line Programming is owned by Guy Lecky-Thompson. Permission to republish Batch Programming ECHO Command in print or online must be granted by the author in writing.




Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo