shell script function with parameters example

Mastering Shell Script Functions with Parameters: An In-Depth Example

By Kevin Davis
AD

Shell scripting is a powerful tool for automating tasks on Unix-based systems. One of its key features is the ability to define functions with parameters, allowing for reusable and modular code. Let's delve into the world of shell script functions with parameters, providing examples along the way.

Before we dive into the specifics, let's ensure we're on the same page. A shell script is a plain text file containing a sequence of commands for a Unix-like operating system to execute. Functions in shell scripts are defined using the `function` keyword, and they can accept parameters just like regular shell commands.

Defining Functions with Parameters

To define a function with parameters in a shell script, you use the `function` keyword followed by the function name and parentheses containing the parameter list. Here's a basic example:

AD
Top 11 Resources to Learn Shell Scripting for Free
Top 11 Resources to Learn Shell Scripting for Free
a computer screen with some type of programming program on it's side, and another screenshot of the code
a computer screen with some type of programming program on it's side, and another screenshot of the code
TechBeamers
TechBeamers

function greet { echo "Hello, $1!" }

In this example, `greet` is the function name, and `$1` represents the first parameter passed to the function. To call this function, you simply type `greet` followed by the parameter, like so: `greet World`.

How to Send Email using Shell Script | Send mails using a Bash Script | Shell Scripting for DevOps
How to Send Email using Shell Script | Send mails using a Bash Script | Shell Scripting for DevOps
Tutorial: Bare-Metal Shell for Kinetis
Tutorial: Bare-Metal Shell for Kinetis
Client Challenge
Client Challenge

Positional Parameters

In shell scripts, parameters are passed to functions as positional parameters. The first parameter is referenced by `$1`, the second by `$2`, and so on. You can access all positional parameters using the special variable `$@`, which represents all the positional parameters passed to the function.

Let's modify our `greet` function to accept multiple parameters and greet each one:

AD

function greet_all { for param in "$@"; do echo "Hello, $param!" done }

the sea shell font and cursive writing is displayed on a white background with seashells
the sea shell font and cursive writing is displayed on a white background with seashells
Parts of a Sea Shell
Parts of a Sea Shell
the anatomy of an animal's shell is shown in three different colors and sizes
the anatomy of an animal's shell is shown in three different colors and sizes
User Case Study: The Art of Modeling Seashell Morphology
User Case Study: The Art of Modeling Seashell Morphology
an animal's shell is labeled in the diagram above it, with labels on each side
an animal's shell is labeled in the diagram above it, with labels on each side
the diagram shows different types of seashells
the diagram shows different types of seashells

Now, when you call `greet_all` with multiple parameters, it will greet each one individually: `greet_all World Hello Scripting`.

Named Parameters

While positional parameters are the most common way to pass arguments to shell script functions, you can also use named parameters (also known as associative arrays) for a more structured approach. To define a named parameter, you use the syntax `name=value`.

AD

Here's an example of a function that accepts named parameters:

the types of seashells and their names are shown in this poster, which shows them
the types of seashells and their names are shown in this poster, which shows them
a poster showing the different types of seashells and how they are used to create them
a poster showing the different types of seashells and how they are used to create them
Building Eclipse Projects with custom external Shell Scripts
Building Eclipse Projects with custom external Shell Scripts
an image of seashell morphs
an image of seashell morphs
the structure of an animal's shell and its major structures, including their main parts
the structure of an animal's shell and its major structures, including their main parts
several different types of seashells are shown in this illustration
several different types of seashells are shown in this illustration
the florida gulf coast shell identification poster
the florida gulf coast shell identification poster
Spiritual Meaning Of Sea Shells, Seashells Meaning, Shell Spiritual Meaning, Shells And Their Meanings, Seashell Meaning, Shell Witchcraft, Beach Magic, Seashell Witchcraft Uses, Shell Meaning Witchcraft
Spiritual Meaning Of Sea Shells, Seashells Meaning, Shell Spiritual Meaning, Shells And Their Meanings, Seashell Meaning, Shell Witchcraft, Beach Magic, Seashell Witchcraft Uses, Shell Meaning Witchcraft
an image of seashells from the 19th century
an image of seashells from the 19th century
the parts of a sea shell
the parts of a sea shell
an iphone screen showing different types of seashells
an iphone screen showing different types of seashells
four different types of seashells are depicted in this illustration, including one shell
four different types of seashells are depicted in this illustration, including one shell
a poster with different types of shells on it's sides and the words shell ids
a poster with different types of shells on it's sides and the words shell ids
the different types of sea shells are displayed on a piece of brown paper with black spots
the different types of sea shells are displayed on a piece of brown paper with black spots

function configure { echo "Server name: $server_name" echo "Port: $port" }

To call this function with named parameters, you use the `-n` and `-p` flags, like so: `configure -n MyServer -p 8080`.

AD

Using Functions with Parameters in Scripts

Functions with parameters are incredibly useful when writing larger scripts. They allow you to break down complex tasks into smaller, reusable functions, making your code more organized and easier to maintain.

Here's an example of a script that defines several functions with parameters and uses them to automate a series of tasks:

#!/bin/bash function backup_file { cp "$1" "/backup/$1" } function compress_file { gzip "$1" } function send_email { echo "Sending email with attachment $1..." # Add your email sending logic here } # Backup a file backup_file important_file.txt # Compress the backed-up file compressed_file="important_file.txt.gz" compress_file "$compressed_file" # Send an email with the compressed file as an attachment send_email "$compressed_file"

In this script, we've defined three functions with parameters: `backup_file`, `compress_file`, and `send_email`. Each function accepts a single parameter, which is used within the function to perform its respective task.

Using Functions with Parameters in Loops

Functions with parameters can also be used within loops to perform repeated tasks. Here's an example that backs up and compresses multiple files using a loop:

#!/bin/bash files_to_backup=("file1.txt" "file2.txt" "file3.txt") for file in "${files_to_backup[@]}"; do backup_file "$file" compressed_file="$file.gz" compress_file "$compressed_file" send_email "$compressed_file" done

In this script, we've defined an array of files to backup and compress. We then loop through this array, calling our functions with parameters for each file.

In the vast landscape of shell scripting, functions with parameters are a powerful tool for creating modular, reusable, and efficient code. By mastering their use, you'll be well on your way to writing complex scripts with ease. So go forth, script away, and happy automating!

AD