Home > Mobile >  Why is : ' ' a comment in Bash?
Why is : ' ' a comment in Bash?

Time:03-08

In bash, you can sort-of do multi-line comments, like this:

: '
echo "You will never see this message :)"
'

But why does it only work out like that? If you do it without space after the colon, an error occurs. And also, if I did what I did above with echo in apostrophes, it still would not be read by the machine.

: '
echo 'You will also never see this message :D'
'

And also without anything around it:

: '
echo You will never see these messages :(

How does this work, and why did everything I look up about multiline comments in bash tell me there wasn't such a thing?

CodePudding user response:

Colon : is a built-in bash command that essentially does nothing.

From the bash documentation:

Do nothing beyond expanding arguments and performing redirections. The return status is zero.

So you can think of : as being a like any other bash command which you can pass arguments to. So it's not a comment, but it sort of works like one because it and every argument passed to it is a no-op. You could accomplish the same thing by creating a "comment" function:

comment () {}

comment echo 'You will never see this message'

The space is required after : because without the space, the whole thing becomes the command name. For example, if you run:

:echo 'You will never see this message'

Bash sees that as running a command called :echo with the argument `'You will never see this message'. It returns an error because there is no such command.

The second part is just how bash handles unmatched quotes. Bash will continue to read data until a matching quote is encountered. So in your multi-line example, you are passing one argument to the : command (in the first example) or padding multiple arguments (in the second example).

CodePudding user response:

This isn't a comment. But it has no effect, so it seems like a comment. But it is parsed and evaluated by bash: you can introduce syntax errors if you use this incorrectly.


Understanding some of the basic building blocks of shell syntax and some built-in commands will help make sense of this.

The shell (such as bash) reads commands, figures out the command name and the arguments from the user input, and then runs the command with the arguments.

For example:

echo hi

Is parsed by the shell as the command echo with 1 argument hi.

Generally, the shell spits things based on spaces/tabs. Which is why it parses echo hi as two things. You can use single quotes and double quotes to tell it to parse things differently:

echo 'foo bar' baz 'ignore me'

is parsed by the shell as the command echo with arguments foo bar, baz and ignore me. Notice that the quotes aren't part of the arguments, they are parsed and removed by bash.

Another piece of the puzzle is the builtin : command. man : will tell you that this is command does nothing. It parses arguments, does directions, but does nothing by itself.

That means when you enter this:

: 'echo hi'

Bash parses it as the command : with argument echo hi. Then the command is run and the output is thrown away. That has no effect, so it feels like a comment (but it really isn't, unlike # which is a comment character).

: '
echo 'You will also never see this message :D'
'

Is parsed by bash as the command : with arguments \necho (that is, a new line character followed by echo ), You, will, also, never, see, this, message :D and then \n (that is, a newline character). Then bash runs this command. That does nothing, so it again behaves mostly like a comment.

  • Related