terminal

Bash 101: Functions and Variables

In this part of the Bash 101 series, we expand on the very basic basics we touched upon in the first article and introduce the concepts of functions and variables. We’ll build on our previous hello world script and then create a new script to illustrate the concepts further. Just to recap the previous articles:

In the first Bash 101 post, we learned about the History of Bash and Terminal.

From the second Bash 101 post, you should remember how to:

    • Echo a phrase
    • Create a new script
    • Run your script
    • Save your script

Let’s get started with Functions and Variables!

1. Variables

If you’ve ever done any work with programming or scripting before the concepts introduced here will be intimately familiar to you. However, for those who haven’t I am going to explain them anyway. Very simply a variable holds data. To use a variable you need to declare it in the script or shell. The syntax for declaring a variable is:

MY_VARIABLE=10

This sets MY_VARIABLE to 10. To view the information stored in a variable, you can type:

echo $MY_VARIABLE

Notice the addition of a money sign ($) before the variable. This distinguishes setting a definition to a variable to getting information to the variable.

2. Types of Variables

While many programing languages have more than one type of variable, Bash only uses one. Many scripts will have one type of variable called int that holds whole numbers, another type of variable called String, which holds words. There’s another called double, which holds floating point numbers (i.e. numbers with decimals in them), and then there are char‘s which hold individual characters.

Bash technically uses only a String integer, but under certain conditions will allow the String integer to be used to perform math equations. We will get a little more involved later in the series when we address computing math in Bash.

3. Functions

Functions are strips of code that are grouped together under a specific name. Once you define a function, you can call it anywhere else in your script. This prevents you from having to type large chunks of code more than once.

Using our Hello World article as an example, if we wanted to say Hello to different worlds or specific people we could put our echo’s into a function and call it again and again. Another example: if you were writing a script that kept track of exam scores, you could write a function that would compute the average grade. Once you get used to them, functions will become an integral part of writing scripts.

4. Declaring Functions in Bash

Declaring a function in Bash is very simple. If you wanted to create a function that would echo hello, you would type:

function hello {
echo hello
}

Note: The name of the function did not have to be hello, but it’s good practice to name a function after what it does. Like caluculateAverage, printData, getInput, etc.

5. Calling Functions in Bash

To call a function later in your script you simply type the name of the function. For example, if I wanted to run the function hello, I would type the following into my script:

hello

Later we are going to address parameters which are useful for transferring information to your functions.

That’s it! You’ve been introduced to the concepts of functions and variables and their implementation in Bash. Look for the next Bash 101 article, where we’ll delve deeper into their usage and write a functional script or two that illustrates these concepts.

Leave a Reply