How to write functions in Bash | Opensource.com

How to write functions in Bash

Reduce redundancy and maintenance in your code by writing functions.

woman on laptop sitting at the window
Image credits : 

CC BY 3.0 US Mapbox Uncharted ERG

x

Subscribe now

Get the highlights in your inbox every week.

When you're programming, you're literally defining a procedure, or a routine, you want the computer to perform. A simple analogy compares computer programming to baking bread: you list ingredients once to set up the work environment, then you list the steps you must take to end up with a loaf of bread. In both programming and baking, some steps must be repeated at different intervals. In baking bread, for instance, this could be the process of feeding a sourdough culture:

STIR=100
SNOOZE=86400

function feed_culture {
  remove_from(pantry)
  add(flour, water)
  stir($STIR)
  sleep($SNOOZE)
}

And later, kneading and proofing the dough:

KNEAD=600
SNOOZE=7200

function process_dough {
  remove_from(proofing_drawer)
  knead($KNEAD)
  return_to_drawer($SNOOZE)
}

In programming, these subroutines can be expressed as functions. Functions are important for programmers because they help reduce redundancy in code, which in turn reduces the amount of maintenance required. For example, in the imaginary scenario of baking bread programmatically, if you need to change the amount of time the dough proofs, as long as you've used a function before, you merely have to change the value of the seconds once, either by using a variable (called SNOOZE in the sample code) or directly in the subroutine that processes dough. That can save you a lot of time, because you don't have to hunt through your codebase for every possible mention of rising dough, much less worry about missing one. Many a bug's been caused by a missed value that didn't get changed or by a poorly executed sed command in hopes of catching every last match without having to hunt for them manually.

In Bash, defining a function is as easy as setting it either in the script file you're writing or in a separate file. If you save functions to a dedicated file, you can source it into your script as you would include a library in C or C++ or import a module into Python. To create a Bash function, use the keyword function:

function foo {
# code here
}

Here's a simple (and somewhat contrived, as this could be made simpler) example of how a function works with arguments:

#!/usr/bin/env bash
ARG=$1

function mimic {
  if [[ -z $ARG ]]; then
    ARG='world'
  fi
  echo "hello $ARG"
}

mimic $ARG

Here are the results:

$ ./mimic
hello world
$ ./mimic everybody
hello everybody

Note the final line of the script, which executes the function. This is a common point of confusion for beginning scripters and programmers: functions don't get executed automatically. They exist as potential routines until they are called.

Without a line calling the function, the function would only be defined and would never run.

If you're new to Bash, try executing the sample script once with the last line included and again with the last line commented out.

Using functions

Functions are vital programming concepts, even for simple scripts. The more comfortable you become with functions, the better off you'll be when you're faced with a complex problem that needs something more dynamic than just declarative lines of commands. Keeping general-purpose functions in separate files can also save you some work, as it'll help you build up routines you commonly use so that you can reuse them across projects. Look at your scripting habits and see where functions might fit.

Puzzle pieces coming together to form a computer screen

'Bash it out' covers basic, medium, and advanced Bash scripting using 16 puzzles.
Command line prompt

Learn how to write custom programs in Bash to automate your repetitive tasks. Download our new eBook to get started.
A person working.

Find out how Bash can help you tackle the most challenging tasks.

About the author

Seth Kenlon
Seth Kenlon - Seth Kenlon is a UNIX geek, free culture advocate, independent multimedia artist, and D&D nerd. He has worked in the film and computing industry, often at the same time. He is one of the maintainers of the Slackware-based multimedia production project Slackermedia.