BASH Comments [Part 3]
Welcome to third article of BASH scripting series. Today we’ll dive into a very important topic, BASH comments. Comment, in programming, is different from the online world of the Internet where you read an article or a blog and then post opinions or applaud the author in the comment section.
BASH Comments
Comment is one or more sentences written in your preferred language explaining how the code works.
Does that mean 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 comments. What does the computer do with the comments? The computer ignores all comments. It won’t even bother to try to read and understand them at all.
We can write BASH comments starting with “#”. Below is an example program with comment –
#!/bin/bash
# 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
#!/bin/ksh
#!/bin/dash
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.
Conclusion
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.