Welcome to chapter three of BASH scripting series. Today we’ll dive into a very important topic in programming called comment. Comment, in programming, is somewhat different from the online world of the Internet where you read an article or a blog and then throw some of your opinions or applaud the author in the comment section.
What is a comment?
Comment is simply one or more sentences written in your preferred language explaining how the code works.
So that means your computer can understand your language? Uh no… But some other human being will because he/she will one day read your code and examine the comment(s) you left out. But what does the computer do with the comment(s), you ask. The computer just ignores all of your comments and skips them. It won’t even bother to try to read and understand them at all.
In BASH, we use “#” to write a comment. Below is an example program with comment:
# a variable named "number" holding value 123 number=123 echo $number
Why comment code?
You might be wondering “I wrote the code and implemented the program because I understood how it should work, then why do I need to explain myself with comments?”
That’s because, for a (real world) large program, we tend to forget things as days, weeks, months, and years fly by. Thus, comments are a great way to make sure you get some good pointer (or recall lost information) when you get lost reading those old big chunks of forgotten code. And if you are the kind of programmer who maintains good quality code, then your comments are sure to help both new and old programmers understand how and why you implemented the program that way.So from next time onwards, we’ll make it a habit of writing comments in our example programs.
The #!/bin/bash line
#!/bin/bash is seen on the first line of every BASH program we’ve ever written but isn’t that line a comment too? Yes, it is. However, it has another special function too. It tells your computer which shell interpreter to invoke.Invoking a shell interpreter is akin to different versions of Linux Ubuntu ie., 16.04, 17.04, 18.04, etc We tend to use the latest version of Ubuntu on our computer to enjoy new & updated features. Similarly, BASH is the updated version of Unix Shell and also the default shell interpreter on most Linux systems. Below are some examples invoking different shell interpreters: #!/bin/sh
However, since our focus is on BASH scripting, we’ll stick to the BASH topic. Moreover, most of our programs might not work on other shell interpreters. But don’t hesitate to try other shells if you’re interested.
This wraps up our comment lesson in BASH scripting series. Not only do we use comments to explain our code but disable certain lines of offending codes too. That we will cover more on later chapters. In the next chapter, we will learn about decisions in BASH.