Quick guide to writing a bash script on the Mac/Linux command-line

Writing a bash script is important for setting up pipelines to process data or run a series of different tools. It also makes your results more reproducible, so you can keep track of what you did and make sure you don’t miss a step next time you are repeating the analysis.

This video (and the text version here) shows how to make a simple bash script that takes an input value and another optional input value from the user.

This video assumes you know how to find the command-line/terminal on your computer and edit a simple text file. You can catch up with this quick bash intro.

1. Open a new file

nano myscript

2. Write the shebang line:

#!/usr/bin/env bash
This tells the system what program to use to run the code, in this case it should look for a program called bash in the computer’s environment and use that to interpret the code we are writing here.
Note: There are several options for shebang lines, but this one is most likely to work across Linux and Unix systems. See this Stack Overflow discussion for details.

3. Write script contents.

Let’s work with a simple example:
echo "HELLO!"
“echo” just means print onto the screen in bash.

4. Make the script executable

chmod +x myscript
This is where you tell the system that this is a script and not just a data file, and that you want to be able to run it as software.

5. Run the script

./myscript
This should print HELLO!
The ./ means that the script is in the same directory. If you go into another directory, you have to specify the relative or whole path to the script, like ~/Desktop/myscript if it is on the desktop. Alternatively, you can make the script globally available by putting it in /usr/local/bin/. What I did is create a bin at ~/bin that contains only my own scripts, added that to my PATH, and then create a symbolic link of each new script into ~/bin/. But that can be the topic of another video.

6. Add an input variable

#!/usr/bin/env bash
NAME=${1?Error: no name given}

If your script needs to take user input, such as a filename, this is how you do it.

7. Now run it:

Without the required parameter:
$ ./myscript
./myscript: line 2: 1: Error: no name given

With the required parameter:
$ ./myscript Tom
HELLO! Tom

8. Add an optional input variable

#!/usr/bin/env bash
NAME=${1?Error: no name given}
NAME2=${2:-friend}
echo "HELLO! $NAME and $NAME2"

Instead of throwing an error if there is no input, we are giving a default value. This is a very common thing to do and allows you to pick some sane defaults for your pipeline.

9. Now run it again:

With one name:
$ ./myscript Tom
HELLO! Tom and friend

With two names:
$ ./myscript Tom Jerry
HELLO! Tom and Jerry

There are other ways to deal with user input, but this is a good start.

I hope that gets you going with creating your own command-line scripts. If you have any questions, you can leave a comment on youtube and I will answer them there.

Related Posts

Video

Introduction to bash for data analysis

Start by opening the terminal (on a Mac, hit command-space and type in terminal. I’m going to show you how to navigate around your file system, but first we are going to make a quick Read more...

Video

Minimum viable product for bioinformatics software: A case study

A case study of prototyping bioinformatics software: Ribbon The way to finish a big software project is to focus on testing the main idea as fast as possible. I applied this idea to finish a Read more...

Video

Is bioinformatics a lucrative career option for biologists?

In this video we talk about supply and demand for bioinformaticians, and why it is hard for professors to recruit bioinformatics post-docs — which is good for PhDs who want to fill that gap.