How do I comment on the Windows command line?

WindowsCommand LineComments

Windows Problem Overview


In Bash, # is used to comment the following. How do I make a comment on the Windows command line?

Windows Solutions


Solution 1 - Windows

The command you're looking for is rem, short for "remark".

There is also a shorthand version :: that some people use, and this sort of looks like # if you squint a bit and look at it sideways. I originally preferred that variant since I'm a bash-aholic and I'm still trying to forget the painful days of BASIC :-)

Unfortunately, there are situations where :: stuffs up the command line processor (such as within complex if or for statements) so I generally use rem nowadays. In any case, it's a hack, suborning the label infrastructure to make it look like a comment when it really isn't. For example, try replacing rem with :: in the following example and see how it works out:

if 1==1 (
    rem comment line 1
    echo 1 equals 1
    rem comment line 2
)

You should also keep in mind that rem is a command, so you can't just bang it at the end of a line like the # in bash. It has to go where a command would go. For example, only the second of these two will echo the single word hello:

echo hello rem a comment.
echo hello & rem a comment.

Solution 2 - Windows

A comment is produced using the REM command which is short for "Remark".

REM Comment here...

Solution 3 - Windows

Sometimes, it is convenient to add a comment to a command line. For that, you can use "&REM misc comment text" or, now that I know about it, "&:: misc comment text". For example:

REM SET Token="4C6F72656D20697073756D20646F6C6F" &REM This token is for localhost
SET Token="722073697420616D65742C20636F6E73" &REM This token is for production

This makes it easy to keep track of multiple sets of values when doing exploration, tests of concept, etc. This approach works because '&' introduces a new command on the same line.

Solution 4 - Windows

It's "REM".

Example:

REM This is a comment

Solution 5 - Windows

: this is one way to comment

As a result:

:: this will also work
:; so will this
:! and this
: ***** and so on ***** :
: // even this \\ :

Above styles work outside codeblocks, otherwise:

REM is another way to comment.

Solution 6 - Windows

Lines starting with "rem" (from the word remarks) are comments:

rem comment here
echo "hello"

Solution 7 - Windows

Powershell

For powershell, use #:

PS C:\> echo foo # This is a comment
foo

Solution 8 - Windows

  1. A single colon without a space after it is enough

  2. Just don't leave comments at the last line in block

Finally, this works:

if 1==1 (
    :comment line 1
    echo 1 equals 1
)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTimView Question on Stackoverflow
Solution 1 - WindowspaxdiabloView Answer on Stackoverflow
Solution 2 - WindowsRobin DayView Answer on Stackoverflow
Solution 3 - WindowsDavid RogersView Answer on Stackoverflow
Solution 4 - WindowsmasfenixView Answer on Stackoverflow
Solution 5 - WindowsZimbaView Answer on Stackoverflow
Solution 6 - WindowsPeterMmmView Answer on Stackoverflow
Solution 7 - WindowsTom HaleView Answer on Stackoverflow
Solution 8 - WindowsAlexander GavriliukView Answer on Stackoverflow