What's the equivalent of #
for Windows cmd
console sessions, to create a comment?
The operating system is Windows XP.
REM
is the standard way:
REM this is a comment
You could also use the double-colon convention commonly seen in batch files:
:: another comment
A single colon followed by a string is a label, but a double colon and anything after it are silently ignored. One could argue that this form is more legible than the REM
command.
Note that both of these methods only work at the beginning of a line. If you want to append a comment to a command, you can use them with the command concatenation character (&
), like this:
dir & REM a comment
dir &:: another one
dir :: comment
?
Dec 12, 2009 at 9:53
if
/for
loops).
rem
actually a command? Wouldn't ::
be better because it itself is being ignored?
REM
is an actual command, but it's embedded in CMD.EXE, so it should be pretty efficient. On the other hand, ::
may be faster, since it ignores everything up the end of line (e.g. it doesn't check for chained commands using &
).
::
can break your code in some cases, such as being the last line in a code block.
You prefix your comment with the word REM.
REM This is a comment.
But if you want your comment printed back to you, you should echo it:
echo This is a comment you'll see.
The REM
command only remarks (i.e. Say something as a comment) the line from being executed. However, if @echo off
is not in the batch file that line will still echo to the screen.
To prevent these lines from being shown you can do one of three things.
I believe the answer provided by bernhard is incorrect.
At least through win 7,
rem comment
in a batch file will print in the output. To suppress printing, use
@ rem comment
or
@rem comment
The same goes for other commands, which by default are echoed to the console.
REM comment
will not print any output to the screen. For a live demonstration, open a Command Prompt and run echo comment
then run the command rem comment
. Notice that the echo
command prints output to the screen while the rem
command did not.
Nov 22, 2014 at 21:52
@REM comment
in my batch scripts to avoid them showing up. I would have suggested editing Bernards answer to include using @ before REM in batch scripts to avoid it showing up rather than posting a whole new answer.
Nov 22, 2014 at 22:07
@ECHO OFF & SETLOCAL ...
. This way you don't need to prepend @
to all lines and you can easily switch to ECHO ON
when something goes wrong and you need to do some testing.
Everything started after exit (with a parentheses) is regarded as comment:
REM CODE
exit /b 0
(Hi
I am a comment
I am also the same
cmd.exe
in Windows is not DOS - it's a full Windows application which just has a similar syntax to that ofcommand.com
# this is a comment