The course is part of this learning path
In this course, you'll learn why and when you'll want to use functions. You'll learn how to create functions as well as how to use them. We'll talk about variables and their scope and how that applies to functions. You'll learn how to use parameters to access the arguments passed to your function. Finally, you'll learn about exit statuses and return codes.
- Obtain a foundational understanding of functions
- Learn how to create and use functions in your scripts
- Learn how to use parameters to access arguments passed to your functions
This course is intended for anyone looking to learn more about bash scripting and shell programming.
To get the most out of this course, you should have some basic knowledge of the command line, but it's not essential.
Functions are really like shell scripts within a shell script. Just like a shell script, a function has an exit status, which is sometimes called a return code. This exit status can be explicitly set by using the return statement and following it with the status you would like to return. If no return statement is used, then the exit status of the function is the exit status of the last command executed in that function. The return statement only accepts a number, and it's only integers between zero and 255 that can be used as an exit status. An exit status of zero signifies the successful completion of a command or a function. A non-zero exit status indicates some type of error. To access the exit status of a function, use a $? right after the function is called. In this snippet, the value of $? will be the exit status of the my_function function. You can use the exit status of a function to make a decision. For example, you could check to see if the exit status is zero, and if it is not, then you know some sort of error occurred. At that point, you could do some sort of error handling for example. Before we wrap up, I wanted to show you an example of a function that you might actually use in one of your shell scripts. The backup_file function will create a backup of a file and place it into the temp directory. I've used this kind of function when my script modifies several files and I wanna make sure I have a copy to view, or even restore, just in case something unexpected happens. In any case, the first line of the function checks to see whether what was passed in is number one, a file, and number two, if it is exists. If it is a file and it does exist, a variable called BACK is created. It starts off with /tmp followed by the basename of the passed in file, the current date, and the PID of this shell script. The basename command removes any leading directory components and returns just the file name. The basename of /etc/hosts is just hosts, for example. The date command is using a nice format of year followed by month, and finally the day, all separated by dashes. $ represents the PID, or process ID, of the currently running shell script. I use this, so if I run the script multiple times on the same day, the PID will be different each time I run the script and thus an existing backup won't be overwritten. In this snippet, the backup_file function is called with /etc/hosts as an argument. This will create a copy of that file and place it in the temp directory as just described. Finally, there's an if statement that examines the return code of the backup function. This is an okay function, I just kinda ran out of space on the screen, so I'll briefly talk through how we can make this better. You can also a better example in the downloadable scripts available along with this course. One thing we can do is change the if statement to an if else statement. If the file exists, let the exit status of the function be the exit status of the cp command. If the cp command returns a non-zero code then our function really has also failed. If we know for a fact that the file doesn't exist, then we can use the else clause to explicitly return a non-zero exit status back to our main script. We can also use the local keyword to make the BACK variable local to this script. We could also improve what happens after the function is called. If we know the function, and thus the backup has failed, you can exit the script with a non-zero exit status. You could also do some other sort of error handling. Maybe you don't want to exit the script but instead just keep track of all the files that failed to get backed up. Also note that you could potentially move this bit of code into the backup file function if you wanted to as well. I'm just using this as an example to demonstrate how you can use the return code of a function to make decisions and access that data with the $? variable. In this lesson, we talked about the concept of DRY, which stands for don't repeat yourself. If you find yourself writing the same bit of code in multiple places in your script, it's a sign that you should use a function. You learned how to create and use functions. You also learned that by default, all variables are global. You learned how to use the local keyword to define local variables that are only available within the function that they are defined. We talked about how to use positional parameters within your functions. Finally, we covered how to use exit statuses with your functions.
Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Amazon.com. Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.