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:
@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:
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:
@echo off
echo Line one
echo.
echo Line three, after a blank line
echo(
set "empty="
echo(%empty%
echo The endLine 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 offturns command echoing off for the rest of the script.@in front of any line hides that one line.@echo offuses it so that even theecho offcommand itself is not shown.echo onturns 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:
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
ByeEach 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 offonly 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:
@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.txtdoes not write1. CMD reads1>as "redirect stream 1", so the file gets theECHO is off.message instead. Put the redirection first,> file.txt echo 1, or add a space,echo 1 > file.txt(which writes1with 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:
@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:
@echo off
echo Step 1: build
rem echo Step 2: deploy to production
echo Step 3: notify the teamStep 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?
- echo
- echo.
- echo ""
- echo off
What does the @ in @echo off do?
- Makes the command run as administrator
- Hides that single line from being echoed
- Turns echo off for all future windows
- Marks the line as a comment
Which line adds a comment after a command on the same line?
- echo Done # comment
- echo Done // comment
- echo Done & rem comment
- echo Done :: comment