• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

Writing Comments in Bash Scripts

Written by Admin, Updated On November 21, 2020
bash, terminal
Writing Comments in Bash Scripts

It’s a best practice to make bash script in a clean and understandable way. You can make you code clean and arrange by indenting, blocks and giving related names of variables and functions. You can also improve the code readability using comments. Comments are human-readable explanation or short description. In this article we will show you how to write comments in bash script.

Giving comment on your bash script will be easy to understand in future and save the time. For example, if you wrote a bash script years ago and want to make changes then it will be easy if you given section wise comments in script. Otherwise it will take time to study the all code and then make changes.

It will also help if the new developers or system administrators who is currently working on the script can understand your code fast and properly.

Generally, comments are description of code, like if you have make custom regex or complex logic in your bash script. Comments will explain why you wrote such code and what it does. Remember that comments should short and meaningful.

Writing Comments in Bash#

In bash, the has # symbol is used to ignore the characters after it. You can use the # to give the comment. It can be added at the beginning on the line or inline with other code:

# Bash comment example.
echo "Test bash code" # Inline Bash comment.

Generally, comments are represented in green color if your text editor supporting syntax highlighting.

At the time of testing bash script the comment is useful to ignore some of code lines or blocks.

# if [[ $i -gt 5 ]]; then
#  echo "value is greater."
# fi

Multiline Comments in Bash#

Bash doesn’t support multiline comments like other programming languages. The easiest way to write multiline comments in Bash is to add single comments one after another:

# first line.
# second line.

You can use the HereDoc as an alternate way. It allows you to pass multiline of input to a command.

<< 'MULTILINE-COMMENT'
    The body of 
	HereDoc is considered
	as comments
MULTILINE-COMMENT

It is recommended that use the single line comments to avoid any issues.

Conclusion#

It is always best practice to add comments while writing bash script. Which will help understand in future and also help to other developers to understand codes. In Bash, everything after the hash mark (#) and until the end of the line is considered to be a comment.

If you have any question or feedback, please leave a comment below.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Increment and Decrement Variable in Bash
Next Article   Bash Select (Make Menus)

Related Posts

  • How to Install SSH Keys on Ubuntu 22.04

    How to Set up SSH Keys on Ubuntu 22.04

    January 7, 2023
  • How to Install Fail2ban on Ubuntu 22.04

    How to Install and Configure Fail2ban on Ubuntu 22.04

    December 5, 2022
  • How to Enable SSH on Ubuntu 22.04

    How to Enable SSH on Ubuntu 22.04

    December 1, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
  • How to Install MariaDB on Debian 11 Bullseye
    How to Install MariaDB on Debian 11 Bullseye March 8, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook