Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Writing Comments in Bash Scripts - DesignLinux

Nov 23 2020
designlinux 0 Comments
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.

Related

Tags: bash, terminal

How to Increment and Decrement Variable in Bash

Prev Post

How to Solve “Sub-process /usr/bin/dpkg returned an error code (1)” In Ubuntu

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 1
  • 606
  • 1,055,378

DesignLinux.com © All rights reserved

Go to mobile version