Yahoo
Advertisement
Advertisement
Advertisement
Advertisement

A gentle introduction to Bash functions

Tux, the Linux mascot, wearing sunglasses and peeking from behind a large terminal window displaying globbing commands.
Lucas Gouveia/How-To Geek

If you're just beginning Bash scripting , you may often find yourself repeating the same commands again and again in your scripts, but a better way exists. I will explain what "DRY" means and how you should use functions to do it.

When I wrote my very first program on Windows , it was a batch script . I essentially winged it, and with minimal understanding, I wrote a very literal set of instructions, peppered with GOTO statements, and repeated myself enough to make the script unreadable.

In programming, DRY means "don't repeat yourself." Functions are the mechanism to achieve that, and I'll show you how.

A primer on Bash functions

I'm a visual learner and believe it helps to see the subject before the technical details. So here's a function:

"foo" is the function name, and it's how we invoke (call) it:

Notice we called the function twice? This is their nature: reusable snippets to make our code "DRY."

Advertisement
Advertisement

We can also use the "function" keyword to define them, but doing so means we cannot use the script in other shells :

Moving on. A more realistic example is to wrap a useful command:

This function makes a backup of a file called "foo.txt" by simply appending "backup" and a date to the file name. We will make the function reusable in the next section.

Functions can be much larger, spanning multiple lines. Think of them as mini, reusable scripts or commands.

Passing arguments to a function

Functions are not very useful unless you can pass them values, and we can—they're called parameters or arguments.

The distinction between these terms is minor. I will call them arguments, but the Bash manual may call them parameters.

Advertisement
Advertisement

Again, an example is best upfront:

The "${1}" and "${2}" are the first and second arguments, respectively. You pass them to "foo" like:

Place "quotations" around words to define their boundaries. Notice that "Hello" also includes a comma and a space? Using quotations this way makes their boundaries clear to Bash.

These are called strings, and I have a more advanced article that covers how to change strings in neat ways.

Anyway, back to functions. "${1}" and "${2}" are called positional parameters in Bash-speak (i.e., parameters with positions). There are multiple ways to access arguments ; some are more advanced, but "${1}" and "${2}" are sufficient for beginners.

Advertisement
Advertisement

Let's use arguments in a real example:

This improves our earlier "backup" function. It takes only one parameter: the filename (or path). Now the function will back up any file you want, which makes it drier than the Sahara Desert.

By enclosing "${1}" in double quotes, it allows Bash to replace (expand) it with the assigned value. Single quotes work differently and cause the string to become literal:

I've changed " cp " to " echo ," so you can see what the "${1}" argument looks like. It's not a variable anymore but a literal value: exactly as you specified it. More on variables below.

There's one more little concept I want to introduce, which will make your code more readable (extremely important):

"local file_path=${1}" is called a variable assignment. A variable is a storage location for a value. We created a variable called "file_path" and stored "${1}" (the function argument) into it. Now "file_path" is equal to "${1}", and we use it in our command instead.

Advertisement
Advertisement

Use variable names this way to describe what the argument is. Your future self will thank you because you can make sense of it at a glance.

Returning values from a function

Returning values from Bash functions is a little different from other languages. In Bash, we can only return an exit code , which is a number that indicates the success status of a command. For example:

The "&&" will "echo" "success" only if "foo" has a successful exit code ("0").

Let's look at an unsuccessful exit code (i.e., the command failed):

In short, the return value of a Bash function is only for exit codes.

Moving on. Let's get a useful value from a function.

Advertisement
Advertisement

In Bash, every output is essentially a string. When a command prints a result (number or text), it's a string. Think of Bash outputs like messages: you ask a command to do something, and it says a message. When we want to return a value from a function, we print a message with "echo":

If the command (e.g., "echo") fails, the function will implicitly return its exit code—but return a custom exit code as you wish.

Let's do something with our result, like we would with any other command:

" tr " will replace "Hello, World!" echoed from "foo" with an uppercase string. This demonstrates that we can use "echo" to return a value and operate on it like any other command.

Advertisement
Advertisement

To drive the point home, we can operate directly on the outputs of any command:

Here the function echoes "Hello, World!", which "tr" transforms into uppercase. Outside the function, we piped the result into " sed " and replaced "WORLD" with "UNIVERSE."


Bash functions are essentially reusable wrappers around commands. You can use them to define complex command pipelines or to perform some detailed work and echo the result. They accept arguments and provide exit codes, just like commands do.

You will most often use Bash functions to make your shell life easier; instead of typing out complex command pipelines, create a function and inject arguments. You'd place these in your bashrc file, which makes them available in your shell, just like any other command. Alternatively, you can use them inside a script on your PATH .

Advertisement
Advertisement
Mobilize your Website
View Site in Mobile | Classic
Share by: