Well, at this point you regret that you did not put some comments ( notes ) into the script to remind you of how the bash script is structured. Not only that Bash comments serve as excellent notes for you or anybody else who might work with your script they may be to some extend also used as a basic bash script debugging tool.
In this tutorial you will learn:
- How to comment on bash command line
- How to write comment bash scripts
- How to create multiple line comments
Software Requirements and Conventions Used
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distribution|
|Other||Privileged access to your Linux system as root or via the |
|Conventions|| # - requires given linux commands to be executed with root privileges either directly as a root user or by use of |
How to write comments in Bash ScriptsThe simplest explanation on how to insert a comment into a bash script is to say that any line except the first line prefixed with the
#is considered as comment by the bash interpreter. Since the same rule applies also to a bash command you can test this definition by simply prefixing any command you with to run on the bash shell terminal. For example try to execute the following commands:
$ echo "Bash Comment" $ # echo "Bash Comment"As you can see the second command has been ignored by the Bash interpreter since it was interpreted as a comment. Commenting bash commands is a great way to save your commands into a bash history without the actual execution.
Next try to create a comment within the actual bash script. All what needs to be done is to prefix each line you wish to comment with
Here is an example:
Next, try to comment some of the lines:
#!/bin/bash greeting="Welcome" user=$(whoami) day=$(date +%A) echo "$greeting back $user! Today is $day, which is the best day of the entire week!" echo "Your Bash shell version is: $BASH_VERSION. Enjoy!"
Depending on you syntax highlighter you might see that the commented lines are now ignored.
#!/bin/bash #greeting="Welcome" #user=$(whoami) #day=$(date +%A) echo "$greeting back $user! Today is $day, which is the best day of the entire week!" echo "Your Bash shell version is: $BASH_VERSION. Enjoy!"
Unfortunately bash does not allow to comment entire block. Instead, if you wish to comment block you will need to comment each line one by one. Fortunately many text editors make your effort easier.
For example using Kate editor allows you to comment multiple lines at once by simply highlighting all required line a pressing CTRL+d shortcut. To uncomment simple press CTRL+SHIFT+D.
Yet, another example is a text based editor VIM as shown on the below video: