A-Z List


Echo

Updated: April 17, 2012

Applies To: Windows Server 2008, Windows Vista

Displays messages or turns on or off the command echoing feature. If used without parameters, echo displays the current echo setting.

For examples of how to use this command, see Examples.

Syntax

echo [<Message>]
echo [on | off]
Parameters

 

Parameter Description

[on | off]

Turns on or off the command echoing feature. Command echoing is on by default.

<Message>

Specifies the text to display on the screen.

/?

Displays help at the command prompt.

Remarks

  • The echoMessage command is particularly useful when echo is turned off. To display a message that is several lines long without displaying any commands, you can include several echoMessage commands after the echo off command in your batch program.

  • When echo is turned off, the command prompt does not appear in the Command Prompt window. To display the command prompt, type echo on.

  • If used in a batch file, echo on and echo off do not affect the setting at the command prompt.

  • To prevent echoing a particular command in a batch file, insert an at sign (@) in front of the command. To prevent echoing all commands in a batch file, include the echo off command at the beginning of the file.

  • To display a pipe (|) or redirection character (< or >) when you are using echo, use a caret (^) immediately before the pipe or redirection character (for example, ^|, ^>, or ^<). To display a caret, type two carets in succession (^^).

Examples

To display the current echo setting, type:

echo

To echo a blank line on the screen, type:

echo.
noteNote
Do not include a space before the period. Otherwise, the period will be displayed instead of a blank line.

To prevent echoing commands at the command prompt, type:

echo off 
noteNote
When echo is turned off, the command prompt does not appear in the Command Prompt window. To display the command prompt again, type echo on.

To prevent all commands in a batch file (including the echo off command) from displaying on the screen, on the first line of the batch file type:

@echo off

You can use the echo command as part of an if statement. For example, to search the current directory for any file with the .rpt file name extension, and to echo a message if such a file is found, type:

if exist *.rpt echo The report has arrived.

The following batch file searches the current directory for files with the .txt file name extension, and displays a message indicating the results of the search:

@echo off
if not exist *.txt (
echo This directory contains no text files.
) else (
   echo This directory contains the following text files:
   echo.
   dir /b *.txt
   )

If no .txt files are found when the batch file is run, the following message displays:

This directory contains no text files.

If .txt files are found when the batch file is run the following output displays (for this example, assume the files File1.txt, File2.txt, and File3.txt exist):

This directory contains the following text files:
File1.txt
File2.txt
File3.txt
Additional references

Command-Line Syntax Key

Tags :


Community Content

Ken A
Advanced: Echo and cmd metacharacters
In addition to a newline, an echo statement is terminated by any of the following meta character strings:<br /><br /><b>&amp;</b> Unconditional command separator. Used to perform successive commands on a single line (useful to extend a single command line parameter to perform as a quasi-batch file.)<br /><br /><b>|</b> Pipe redirection. Like the &amp; character, everything to the right of the character is a separate command. Unlike the &amp; character, the second command executes simultaneously with the first, though most commands used with a pipe typically wait for input from the first.<br /><br /><b>&amp;&amp;</b> "True" Conditional command separator. The command following this separator will be executed if the preceding command returned errorlevel 0.<br /><br /><b>||</b> "False" Conditional command separator. Executes if the preceding command returned an errorlevel other than 0. To branch the a command with both a '&amp;&amp;' command and a '||' command (like an if..then..else statement), use the following syntax:<br /><br /> (Command 1 &amp;&amp; Command 2) || Command 3 [&amp; Command 4]<br /><br /> Note that Command 4 will execute regardless of the return status of Command 1. Any of the above commands could contain an echo statement.<br /><br /><b>)</b> End of command block. The '(' that begins the block <i>must</i> come before the 'Echo' for its corresponding ')' to be recognized as a metacharacter. Although a ')' marks the end of a command, a new command doesn't actually begin until a newline character or one of [ &amp; | &amp;&amp; || ] is encountered, so any undirected text appearing between the ')' and a command separator will cause a syntax error.<br /><br /> Other meta characters that affect Echo statements:<br /><br /><b>&lt;</b>, <b>&gt;</b>,<b> &gt;&gt;</b> Redirection metacharacters don't terminate an echo statement, though they are typically found at the end of a command statment. The contents of a file or handle specified after the input redirector aren't actually sent to the output of an Echo statement. Nevertheless, if the word following any redirector isn't a valid file name or handle, an error message will replace the output of the echo statement (to work around this, try adding '&lt;&amp;2' to the end of the statement.)<br /><br /><b>^</b> An escape character is a modifier that causes any meta character that follows to be treated as a literal. In Unix shells and many scripting languages, the backslash character '\' is a commonly used as the escape character. Since pathnames in Windows are delimited by a bashslash, Window commands use '^' instead (except Findstr, which does use '\' to escape in regular expressions).<br /><br /> Escaped meta characters, when passed to a child CMD process, become meta characters once again, thus making it possible to execute multiple commands and redirect I/O in a child process, while being interpreted as a single command to the parent process.<br /><br /> Since newline characters are also meta characters, escaping them--by placing an escape character at the end of a line--effectively nullifies them. Thus one command can span multiple lines, provided each line ends with a '^' and the following line doesn't start with a metacharacter.<br /><u><br /> Examples:</u><pre><br />Echo compressing...<br />(upx -9 -k mrt.exe &amp;&amp; del /q mrt.ex~) || Echo A problem occurred.<br /> Echo.&amp; Echo This will output &amp; Echo four lines of text &amp; Echo.<br /> Echo This, however, will only ^<br />output on a single line.<br /><br /> Echo Hello&lt;" This part is actually ignored "&lt;. World&lt;&amp;2</pre><br /> Putting it all together, <br /><pre><br />For /f "tokens=*" %Q in (<br />'echo 2000^&amp; ^<br /> echo XP^&amp; ^<br /> echo 2003 Server^&amp;^<br /> echo Vista') do @(<br /> (<br /> echo.&amp; Ver | Findstr "%Q" &gt;nul &amp;&amp; (<br /> echo You are using Microsoft Windows %Q.)<br /> ) || ( rem :: %Q wasn't found in 'Ver' ::<br /> echo This also works with Windows %Q.<br /> )<br /> )</pre><br /><br /><br />

Evari
Write to text file
echo can be used to create/write/append a text file<br /><br />echo The Red Hot Chili Peppers are: &gt; RHCP.txt<br />echo Anthony Kiedis &gt;&gt;RHCP.txt<br />echo John Frusciante &gt;&gt;RHCP.txt<br />echo Michael "Flea" Balzary &gt;&gt;RHCP.txt<br />echo Chad Smith&gt;&gt;RHCP.txt<br /><br />will write the text file below<br />"The Red Hot Chili Peppers are: <br />Anthony Kiedis <br />John Frusciante <br />Michael "Flea" Balzary <br />Chad Smith"<br /><br /><br /><br />

Page view tracker