Skip main navigation

Adding Comments to Bash Scripts

how to add comments to scripts
© Wellcome Genome Campus Advanced Courses and Scientific Conferences

Comments are one of the most important things to consider when you are writing your scripts.

Making sure that you add comments throughout your scripts is just one of many good practices you should be keeping, no matter which programming language your scripts are written in.

Comments help you keep track of what your script is doing. They are useful because they allow you to leave internal notes throughout your script to remind you of things like what the next command will do or its expected output. You’re essentially annotating your scripts.

Why do I need to add comments to my script, I already know what it’s doing?

It’s not uncommon to write a script and then not come back to it for several months, at which point you will need to familiarize yourself with the code all over again. Comments simplify that process!

In Bash, we can identify comments using the # symbol. Every line that starts with the # sign is for our reference only. In Bash, anything after the # is not interpreted or executed. The only exception to this rule is the shebang which goes at the start of our script.

Here is an example comment:

 # This is a comment

OK, let’s try adding some comments to a script:

#!/usr/bin/env bash

# Say hello to the world
echo "Hello world!"

If we ran this script the output would be:

Hello world!

As you can see, our comment was not sent to the terminal.

Comments are a really useful way to describe complex functionality so that you, and other people, can find their way around your scripts. However, comments are only as good as the information they contain, keep them clear and keep them concise!

© Wellcome Genome Campus Advanced Courses and Scientific Conferences
This article is from the free online

Bioinformatics for Biologists: An Introduction to Linux, Bash Scripting, and R

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now