105

What's the equivalent of # for Windows cmd console sessions, to create a comment?

The operating system is Windows XP.

That Brazilian Guy
  • 6,834
  • 10
  • 64
  • 102
Andrew Grimm
  • 2,710
  • 6
  • 30
  • 37

5 Answers5

127

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
efotinis
  • 4,164
  • 1
  • 22
  • 22
  • 2
    Thanks. Does this only work if it's the first thing encountered, as opposed to doing something like `dir :: comment`? – Andrew Grimm Dec 12 '09 at 09:53
  • 1
    First an [useful link](http://www.robvanderwoude.com/comments.php). Then, double colon comments doesn't seem to work inside code blocks (code between parentheses, like in `if`/`for` loops). – x-yuri Jun 20 '14 at 11:54
  • @efotinis, Is `rem` actually a command? Wouldn't `::` be better because it *itself* is being ignored? – Pacerier Feb 03 '15 at 12:29
  • @Pacerier, yes `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 `&`). – efotinis Feb 03 '15 at 19:06
  • 2
    Keep in mind that using `::` can break your code in some cases, such as being the last line in a code block. – bryc Aug 15 '15 at 12:05
  • As far as I can tell, `::` within the if block of an `if` statement also breaks the code – René Nyffenegger Feb 25 '19 at 13:24
  • I also vaguely recall there being a bug that was still around in Windows 7's cmd.exe where using `::` and multi-line codeblocks based on parenthesis (`if`, `for`, etc) resulted in batch scripts breaking if they exceeded a certain length. It happened even without the `::` comments being within codeblocks and presented as cmd.exe seeming unable to read a large portion of the file. – Charles Grunwald Jan 04 '20 at 03:55
19

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.
Bernhard Hofmann
  • 594
  • 2
  • 7
  • 20
  • Accepting as working, but I'd only have been slightly more surprised if the answer was BTW, like in http://lolcode.com/specs/1.2#comments – Andrew Grimm Dec 12 '09 at 08:06
11

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.

  1. Add @echo off to the batch file:
    If you want to hide all commands as well as the REM lines add @echo off as the first line in the batch file.
  2. Change the REM to @REM:
    If you want to have the commands shown when the batch file is run, but still want to hide the REM lines type @REM instead of REM.
  3. Use ::(i.e. invalid lable) instead of REM:
    Finally, using the :: as the remark command instead of REM also prevents the echo of the remarked line.

source

Premraj
  • 2,116
  • 2
  • 18
  • 25
2

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.

pbpb
  • 79
  • 5
  • The command `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. – I say Reinstate Monica Nov 22 '14 at 21:52
  • I always use `@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. – Robin Hood Nov 22 '14 at 22:07
  • 3
    @RobinHood, batch files echo all commands by default (for debugging purposes), that's why it's customary to start them with `@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. – efotinis Feb 03 '15 at 19:11
0

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
Wasif
  • 7,984
  • 2
  • 19
  • 32