Reputation: 17316
In bash GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu) or any stable one,
You can use #
or :
to comment
What's the difference between these 2 and usefulness?
I noticed #
comment makes the whole line a comment, while :
scope/effect is only until it reaches the first \n
or ;
character (in a given line).
[gigauser@someserver ~]$ # this is a # comment; echo this will not print
[gigauser@someserver ~]$ : this is a : comment; echo this will print
this will print
In the following code, why the last 2 comments didn't work as expected to treat them as a comment then (albeit ::
works if it comes after a :
?
[gigauser@someserver ~]$ #
[gigauser@someserver ~]$ ##
[gigauser@someserver ~]$ ####
[gigauser@someserver ~]$ ####
[gigauser@someserver ~]$ : : : : : : : : : :
[gigauser@someserver ~]$ #
[gigauser@someserver ~]$ # # #
[gigauser@someserver ~]$ ####
[gigauser@someserver ~]$
[gigauser@someserver ~]$ :
[gigauser@someserver ~]$ : :
[gigauser@someserver ~]$ : : : : : : : : : : : : ::::::::
[gigauser@someserver ~]$ : : :::::
[gigauser@someserver ~]$
[gigauser@someserver ~]$
[gigauser@someserver ~]$ ::
-bash: ::: command not found
[gigauser@someserver ~]$ :::::
-bash: :::::: command not found
[gigauser@someserver ~]$
Upvotes: 1
Views: 506
Reputation: 125708
:
isn't a comment at all, it's a command that doesn't do anything. The most important difference is that since it's a command, any shell syntax after it will be interpreted and acted on. For example:
: Check whether x > 1
Here the > 1
part will be interpreted as an output redirection, so it'll create a file named "1" and redirect the (empty) output to it. If it'd been x < 1
it would've tried to open "1" for input and (probably) failed.
: Check permissions on the user's home directory
Here the '
will be interpreted as starting a single-quoted string (which'll be an argument to :
), and will probably wreak havoc with how the rest of the script is parsed.
: Test for completion (again)
Produces -bash: syntax error near unexpected token `('
The :
command is sometimes useful if you want parameter expansions to be performed (because of their side effects), but don't want to do anything with the result.
: ${VAR:=defaultvalue}
Here, if $VAR
isn't already set to something nonblank, the :=
modifier will set it to "defaultvalue". This is basically a shorthand for
if [ -z "$VAR" ]; then
VAR=defaultvalue
fi
Edit:
As AKS pointed out with a link in the comments, one effect of :
is that you can make block comments with it and a here-document with a quoted delimiter. That makes a functional difference from #
.
: <<'COMMENT'
This block doesn't do anything, effectively a multi line comment block.
It'll also not get identified by linters and syntax highlighters as a comment.
COMMENT
Regular comments would have to start each line with #
.
(Note: contrary to what the link says, you can use either single- or double-quotes on the here-doc delimiter, i.e. either <'COMMENT'
or <"COMMENT"
. Without some sort of quoting on the delimiter, the shell will try to parse and expand any $
or backtick expressions it finds in the document, which can again cause trouble.)
Upvotes: 7