Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between : and # for Bash comments

Tags:

bash

Today I saw a bash script use a colon to denote a comment. What's the difference between using a colon and a hash mark?

: This is a comment.
# This is also a comment.

For one, I know you can't use a colon for a trailing comment:

cd somedir : This won't work as a comment.

But the fact that the above example works has me puzzled about how : is evaluated.

like image 804
fny Avatar asked Sep 23 '17 00:09

fny


People also ask

What's the difference between single and double quotation marks?

General Usage Rules In America, Canada, Australia and New Zealand, the general rule is that double quotes are used to denote direct speech. Single quotes are used to enclose a quote within a quote, a quote within a headline, or a title within a quote.

What is the difference between single and double inverted commas?

Double quotation marks (in British English) are used to indicate direct speech within direct speech (use single inverted commas for direct speech and double quotation marks to enclose quoted material within).

What's the difference between apostrophe and quotation marks?

They are two entirely different punctuation symbols. Single quotes are limited to one real function in written U.S. English, which is to indicate a quotation within a quotation. Apostrophes, on the other hand, are used to denote possessive form and to indicate omission.

What is the difference between inverted commas and quotation marks?

Inverted commas are mainly used to introduce quotations or direct speech. This is why the name quotation mark is used as a synonym for inverted commas. A direct quote (the exact words spoken or written) must be always written inside quotation marks.


1 Answers

: is simply an alias for true, and true ignores its arguments:

# Does nothing:
true foo bar etc hello

# Does the same:
: foo bar etc hello

It's not a comment and should never be used as a comment, because all its arguments are still parsed and evaluated:

: This "comment" actually executes this command: $(touch foo)
ls -l foo

or like here, where StackOverflow's syntax highlighting picks up that the command in the middle is actually just text, even if a human doesn't:

: The command below won't run:
echo "Hello World"
: because the surrounding "comments" each contain a ' characters
like image 117
that other guy Avatar answered Oct 11 '22 13:10

that other guy