The hash character (#)

This represents a comment. Each line beginning with # is taken as a comment and not interpreted by the shell. Let's have a look at the following script:

#!/bin/bash
# I am a comment at the beginning of a line'
ls # I am a comment after a command
#I am a comment preceeding a command and so it is not interpreted ps

The first pound sign is not really a comment, but it is associated to the following exclamation mark and is interpreted as a sha-bang. The second line shows a typical comment line, the third a comment after a command, the fourth line is still a comment, and the ps command is not interpreted and executed. Let's run it:

zarrelli:~$ ./comment.sh
* 1 2 3 comment.sh

We see the output of the ls command, but not of ps as expected. Also notice that the comments are not printed to stdout. Since we use it inside the code to comment, it is not something to be shown at runtime. Let's add a couple of lines:

echo # I am a comment but you cannot see me
echo # I am a comment but you can see me

Run the script again:

zarrelli:~$ ./comment.sh
* 1 2 3 comment.sh
# I am a comment but you can see me
zarrelli:~$

As you can see, the first echo command creates a blank line and the comment is not taken in account; so, it is as if we had no argument to the echo command. The second comment is even more interesting. We escaped the pound sign placing a back slash in front of it. So, being escaped the pound sign is just a pound sign followed by a bunch of characters, and all together, they get printed to stdout as the echo command arguments. So, be careful since you will find the pound sign used with different meaning, as we already saw in the paragraphs dealing with a parameter substitution and a pattern matching on variables.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.252.8