Issue
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?
Solution
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).
Answered By - Nikolas Stevenson-Molnar Answer Checked By - Cary Denson (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.