TExcelTAD
Home › Windows CMD & Batch › echo & rem

echo & rem

echo is the command you will use most in batch files: it prints messages, writes lines into files and controls whether CMD shows the commands it runs. rem (and its unofficial cousin ::) adds comments that CMD ignores.

Printing text

Everything after echo and one space is printed exactly as written. There is no need for quotes, and quotes you do write are printed too:

cmd · try it yourself
@echo off
echo Hello, world!
echo   Two leading spaces are kept.
echo "Quotes are printed as well"
set city=Lisbon
echo The meeting is in %city%.
Hello, world!
  Two leading spaces are kept.
"Quotes are printed as well"
The meeting is in Lisbon.

Variables written as %name% are replaced by their value before echo runs, so echo never sees the % signs. Special characters such as &, |, < and > must be escaped with ^ (echo Fish ^& Chips); see CMD Syntax.

Printing an empty line

echo on its own does not print an empty line. It reports whether command echoing is on or off:

cmd
C:\Users\ada>echo
ECHO is on.

To print a blank line, put a character directly after echo, with no space. echo. is the traditional form; echo( is the most robust one, because it also works when the text comes from a variable that might be empty:

cmd · try it yourself
@echo off
echo Line one
echo.
echo Line three, after a blank line
echo(
set "empty="
echo(%empty%
echo The end
Line one

Line three, after a blank line


The end

echo on, echo off and @

By default CMD echoes each command of a batch file to the screen, prompt and all, before running it. That is handy for debugging but noisy, so batch files start with @echo off:

  • echo off turns command echoing off for the rest of the script.
  • @ in front of any line hides that one line. @echo off uses it so that even the echo off command itself is not shown.
  • echo on turns echoing back on, for example around a section you are debugging.

Here is what a two-line batch file without @echo off looks like when you run it from a prompt:

cmd
C:\Users\ada>type hello.bat
echo Hello
echo Bye

C:\Users\ada>hello.bat

C:\Users\ada>echo Hello
Hello

C:\Users\ada>echo Bye
Bye

Each command is printed after the prompt before its output appears. With @echo off as the first line, the same script prints just Hello and Bye.

Tip: Interactive Command Prompt windows always echo what you type, because you typed it. echo off only matters inside batch files.

Writing lines into a file

echo combined with > (create or overwrite) and >> (append) is the quickest way to create small text files. Write the > right after the text, with no space, or the space ends up in the file:

cmd · try it yourself
@echo off
mkdir demo & cd demo
echo [settings]> app.ini
echo theme=dark>> app.ini
echo lang=en>> app.ini
type app.ini
(
  echo Shopping list
  echo - bread
  echo - coffee
) > list.txt
type list.txt
[settings]
theme=dark
lang=en
Shopping list
- bread
- coffee

The parentheses group three echo commands so that one > captures all of their output.

Warning: echo 1> file.txt does not write 1. CMD reads 1> as "redirect stream 1", so the file gets the ECHO is off. message instead. Put the redirection first, > file.txt echo 1, or add a space, echo 1 > file.txt (which writes 1 with a trailing space). The Redirection lesson explains stream numbers.

Comments with rem and ::

rem (short for remark) marks the rest of the line as a comment. Many scripts use :: instead, which is really a label that can never be jumped to, and CMD skips it slightly faster:

cmd · try it yourself
@echo off
rem Backup script, version 2
rem Author: Ada
:: Settings
set target=D:\Backup
echo Target is %target%
echo Done & rem a comment after another command needs "& rem"
Target is D:\Backup
Done

A comment at the end of a line needs & rem, because everything after echo would otherwise be printed. Prefer rem inside parenthesized blocks (if, for): a :: label inside a block can cause confusing syntax errors, especially as the last line before ).

Form Where it works Notes
rem text everywhere The official comment command. Shown when echo is on.
:: text outside ( … ) blocks A label that can't be used; never shown. Avoid inside blocks.
command & rem text end of a line Needed for comments after a command.

Commenting out code while testing

Putting rem in front of a line is the usual way to disable it temporarily. Variables inside the comment are still expanded, but nothing runs:

cmd · try it yourself
@echo off
echo Step 1: build
rem echo Step 2: deploy to production
echo Step 3: notify the team
Step 1: build
Step 3: notify the team

Learn more: Batch Files · Bash echo

Test yourself

Which command prints a blank line in a batch file?

  1. echo
  2. echo.
  3. echo ""
  4. echo off

What does the @ in @echo off do?

  1. Makes the command run as administrator
  2. Hides that single line from being echoed
  3. Turns echo off for all future windows
  4. Marks the line as a comment

Which line adds a comment after a command on the same line?

  1. echo Done # comment
  2. echo Done // comment
  3. echo Done & rem comment
  4. echo Done :: comment