Programming Field

Echo - DOS/Command Prompt Reference

Prints text. Also toggles the mode whether to print commands to be executed in a batch file.

Syntax

echo <text>
echo [ON | OFF]

Options

<text> Specifies text to be printed to the screen.
ON | OFF

Sets whether to display the text such as ‘C:\>’ in the prompt (echo-back). The text will be displayed for ON, and not displayed for OFF. (Upper- and lower-case do not matter.)

If Echo is executed without ON and OFF, and without any text (i.e. just executed with ‘echo’ only), the current echo-back mode will be printed.

Details

About Echo

Usage of Echo

Echo is one of the basic command and is used for printing text in a batch program. The printed text is always followed by a newline. You cannot print strings without newlines.

* [Windows NT series] [Extensions] Using ‘set /P XXX="<text>" < NUL’ will print text without a newline. (‘set /P’ shows prompt without newlines, and the input for ‘set /P’ is provided by redirection.) Note that the environment variable ‘XXX’ will be cleared for this syntax.

To print an empty line with Echo, use ‘echo.’ (‘echo’ and one period). ‘echo ’ (‘echo’ and one space) is treated as omission of ON and OFF and will print ‘ECHO is on/off’. (see Sample 3)

* For one character following echo, some symbols such as a period, a colon ‘:’, and a semicolon ‘;’ will be ignored as well as a space. You can execute ‘echo.test’ instead of ‘echo test’. (When ‘echo.exe’ is executed, a file ‘echo.exe’ is executed if available, but ‘exe’ text is printed if ‘echo.exe’ is not available.)

With using the pipe ‘|’, ‘>’, and ‘>>’, any strings can be written to files or be passed as input of program execution.

[Windows 10 or later] By using escape sequence directly, you can control the prompt such as changing the color. Please see also details of Color page.

‘echo off’ and echo-back mode

Echo command can be used to print text, but also be used to toggle echo-back mode by specifying ‘ON’ or ‘OFF’. Echo-back is a feature which command-lines are printed just before execution in a batch file etc.

Echo-back is on by default. If on, the screen would be filled by command-line outputs, so you can set echo-back off by using ‘echo off’. Conversely, you can keep echo-back on (by not using ‘echo off’, or by using ‘echo on’ explicitly) to see command-lines written in a batch file. (Please see samples of Prompt command.)

In batch files, ‘@echo off’, which is a combination of ‘@’ character and ‘echo off’, is commonly used. This means, if ‘@’ is not used (i.e. just ‘echo off’ is used), the command-line ‘echo off’ is printed, so ‘@’ character, which suppress output of one command-line, is used.

Samples

Sample 1

echo Hello!

Prints ‘Hello!’ text to the screen.

Sample 2 (Batch file)

@echo off

Suppresses strings such as ‘C:\>’. If this is not used in batch program, all execution commands will be printed to the screen, which causes hard to see. And, if you forget ‘@’ character, the command-line ‘echo off’ will be printed.

Sample 3 (Batch file)

@echo off
echo Other programs are accessing the file.
echo Please retry after finishing those programs.
echo.
echo Command failed.

This program will print following text:

Other programs are accessing the file.
Please retry after finishing those programs.

Command failed.

If you write a program as following:

@echo off
echo Other programs are accessing the file.
echo Please retry after finishing those programs.
echo 
echo Command failed.

the program will print as following:

Other programs are accessing the file.
Please retry after finishing those programs.
ECHO is off.
Command failed.

Sample 4

echo Home: %HOMEDRIVE%%HOMEPATH%> dirinfo.txt
echo Public: %PUBLIC%>> dirinfo.txt

Creates a new file ‘dirinfo.txt’ and writes text. The text comes from values of environment variables.

See also