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

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education