From the course: Learning Bash Scripting

Unlock this course with a free trial

Join today to access over 24,800 courses taught by industry experts.

Using functions

Using functions

- [Scott] During your script development, you may find yourself repeating the same set of commands, or writing very similar commands while changing just one or two parameters. It's a good practice to not repeat yourself if you don't have to. And keeping scripts organized without duplication makes them a lot more maintainable too. To help organize code, we can use functions, which let us write a set of commands once and then refer to that code by name whenever we need to run it. To create a function, we'll give the function a name, in this case the name fname, and follow that with open and close parentheses. And then we'll add a set of braces to enclose commands that will make up the function. In older scripts, you might see functions defined with a function keyword, and those definitions may or may not have the parenthesis after the function name. Functions need to be declared before they're used. And in many cases you'll…

Contents