Instructor: I've already created a script file called functions.sh and I've given that execute permissions using the chmod commands.
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.
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.
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.
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.
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.
To verify it works, I'll echo, "The greeting is" greeting. Let's run that now. Cool, we see that it's working.
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!"
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.
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.
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.
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.
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.