Understand and Use Functions in Bash

Cameron Nokes
InstructorCameron Nokes
Share this video with your friends

Social Share Links

Send Tweet
Published 7 years ago
Updated 6 years ago

In this lesson, we'll go over how bash functions work. Bash functions work like mini bash scripts--you can pass parameters and invoke them just like a bash command. You can also define local variables within a bash function using the local keyword. Local variables follow similar scope rules present in most programming languages.

Instructor: [00:00] I've already created a script file called functions.sh and I've given that execute permissions using the chmod commands.

[00:08] Defining a function in Bash is somewhat similar to other programing languages. We give it a name. We'll create a greet function, and we give it two parentheses and an opening and closing curly brace. In the function's block is where we define commands that we want to run.

[00:24] We'll do an echo "Hello, World!" here. Then to invoke that function, we invoke it just like we invoke any Bash command. Let's run this. Cool, we can see that's working. Let's see how we pass arguments to a function.

[00:41] This works just like passing arguments to a script from the command line. In fact, you can think of functions as mini scripts. If I wanted to pass a different greeting here, we'll say "Howdie" instead of "Hello, World!" Replace hello with the 1 variable. Just like a script, this 1 is set to the first parameter that is passed to the command or the function in this case.

[01:07] Let's run that. We can see that it works as expected. It might be tempting to want to name the parameters between these parentheses, but the parentheses don't serve any purpose other than to indicate to the Bash compiler that it's a function definition, not a command invocation.

[01:23] To store the value that is outputted from the function, we can use the command substitution syntax. If I wanted to save this "Howdie, World!" as string in the variable, we'll give the variable a name, and then I'll use the command substitution syntax here, which is the dollar sign with the two parentheses.

[01:43] To verify it works, I'll echo, "The greeting is" greeting. Let's run that now. Cool, we see that it's working.

[01:55] Note that anything that's echoed in this function will be captured in this variable when you use the command substitution syntax. If you echoed something else up here, write that "Hey," we run that again, we can see that it's going to do "Hey," new line, "Howdie, World!"

[02:14] Something to keep in mind when you're trying to return values from a function. You want to be careful with how you use your echo. Let's clear everything.

[02:25] Functions can access variables from the outer scope, as well as to clear variables that are local to their inner scope. For example, let's create a variable that's at the top level here. This will be accessible to everything within this script.

[02:38] Let's call it global. We'll give it the value of 123. Let's create a function here. We'll call it test. In here, let's access that global. We'll say global is equal to, and then let's do clear a local variable. We'll use the local keyword for that, and I'll call my local variable local var. I'll make this a string. Let's echo that out as well.

[03:02] Now let's run our test function. To verify that the scoping rules work as expected, let's run this again, which should work. Let's try echoing out our local var again, which should not work. This should be an empty value right here.

[03:20] Let's run the function start.sh. We can see our global is working. The local variable is working as expected in the test function. Outside of it, we echoed out the global, which this still has access to. The local variable there is empty, like we expect.