Skip main navigation

New offer! Get 30% off one whole year of Unlimited learning. Subscribe for just £249.99 £174.99. New subscribers only. T&Cs apply

Find out more

Bash Functions

Learn more about bash functions and the features.

When you’re writing Bash scripts, you’ll often find that there are repetitive tasks. Instead of copying and pasting the same code to multiple places in your scripts, try using a function.

Functions are a great way of producing reusable code! They are essentially a set of commands that can be called as many times as you need in your script. What’s even better is that functions are not unique to Bash, they’re a core component of many other programming languages too.

Bash function syntax is pretty straightforward. We start off by defining the function name, followed by parentheses. The commands that we want to execute are found between the curly brackets and are known as the body of the function.

function my_function() {
 #some code
}

 

There is an alternative syntax where you don’t have to prefix that first line with function:

 

my_function() {
 #some code
}

 

However, it is much easier to pick out our functions if we use the previous syntax. It’s also a good idea to make sure that the names of your functions are relative and descriptive so that you can quickly see what they’re going to do.

 

When we define a function, we are not executing it. Let’s use a simple toy example to demonstrate where we are using a function to return “Hello world” back to the terminal. We’ll call our function say_hello. You can see that we don’t execute the code in the function body until we specifically call (or execute) the function with say_hello.

 

#!/usr/bin/env bash
 
# Define a function to print "hello world"
function say_hello() {
 echo "Hello world"
}
 
# Execute the say_hello function
say_hello

 

This would output:

 

Hello world

 

We can adapt out function to take arguments using reserved variables. To access the first argument given to the function, we use the variable $1. Let’s tweak our script to use an argument, our name, that is provided to our say_hello function.

 

#!/usr/bin/env bash
 
# Define a function to print "hello world"
function say_hello() {
 echo "Hello $1"
}
 
# Execute the say_hello function
say_hello "Victoria"

 

This would output:

 

Hello Victoria

Functions are one of the best ways to produce scalable and readable code. One general rule of thumb is not to make your functions too big. You can call a function within a function, so, break each function down into small, clear tasks.

Your task

Create a function called file_exists taking the first argument (a filename) which it uses to see if the file exists. If it doesn’t, return “File does not exist: “, followed by the filename.

Note: you can use the “!” notation when you want to check a negative.

If file exists:

if [[ -e $1 ]]

If file does not exist:

if [[ ! -e $1 ]]

Please try to answer the questions yourself first and then compare the results with other learners. Once you’ve tried the exercise, you can find solutions in the download area.

© 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