2. echo, Comments & Script Structure¶
What & why¶
echo is how a script talks to you, and it has more edge cases than any command this simple deserves. This lesson covers printing text cleanly, the two comment styles, and the handful of commands that give a script a readable shape.
echo basics¶
Note %% — a literal percent sign in a script is written %%. A single % starts variable expansion.
Print a blank line¶
No space before the dot. echo (with a trailing space and nothing else) prints ECHO is off. instead — a classic surprise. echo., echo:, echo( all print an empty line; echo( is the most robust.
echo on / echo off / bare echo¶
echo &rem prints whether echo is on or off
echo on &rem turn command-echoing back on (for debugging)
echo off &rem turn it off again
Characters that need escaping¶
The shell metacharacters & | < > ^ ( ) must be escaped with a caret ^ when you want them printed literally:
Inside double quotes on an echo line the quotes are printed too, so quoting is not the way to escape here — use ^.
Comments¶
rem This is the official comment command.
:: This is a label that can never be jumped to, used as a comment.
:: is a trick: it's a broken label, and cmd skips it. It's faster to type and visually cleaner, but has two hazards:
- Inside a
forloop or a parenthesised block,::can cause errors. Useremthere. ::on the same line as code doesn't work as a trailing comment. Use&rem:
Giving a script structure¶
@echo off
setlocal
title Deploy Tool
color 0A
cls
echo ============================
echo Deploy Tool v1.0
echo ============================
echo.
echo Step 1: checking prerequisites...
echo Step 2: copying files...
echo.
echo Done.
endlocal
pause
| Command | Purpose |
|---|---|
title Deploy Tool |
Sets the window title bar. |
color 0A |
Background 0 (black), text A (bright green). color with no args resets. |
cls |
Clears the screen. |
setlocal / endlocal |
Isolates variable changes to this script — Intermediate Lesson 5. Get in the habit now. |
Multi-line output¶
Every echo is its own line. For a block, either repeat echo, or use one redirect:
(
echo Line 1
echo Line 2
echo Line 3
)
rem Or build a here-doc-ish file:
(
echo [settings]
echo verbose=1
) > config.ini
Common mistakes¶
echo.with a space (echo .) prints a literal dot. No space.echo %PATH%truncating — it doesn't truncate, but ifPATHcontains)inside a parenthesised block it breaks parsing. Quote or restructure.::insidefor ( … )producingThe system cannot find the drive specified.Userem.- Unescaped
&in an echo —echo Ben & JerryrunsJerryas a command. - Trailing spaces after a value you
echointo a file — they're included. Watch line ends.
Exercises¶
- Print this exact block using
echo, including the blank line: - Print the literal string
A & B | C > Don one line. - Write a script that sets the window title to
Backup, clears the screen, prints a 3-line banner, thenpauses. - Write
notes.batwith aremheader comment describing what it does, three::comments between sections, and one&remtrailing comment. Run it with@echo offremoved and confirm no comment lines execute. - Make
echoprint100%correctly (hint:%%).
Recap¶
echo textprints;echo.(no space) orecho(prints a blank line.- Literal
%is%%; escape& | < > ^ ( )with^. remis the safe comment;::is cleaner but avoid it insidefor/blocks.title,color,cls,setlocal/endlocalgive a script shape.
Next: Variables & set →