Shell Script Functions and Arguments

Ayorinde Olanipekun, PhD
11 min readApr 11, 2023

--

Shell Scripting

Shell Scripting is a programming technique that involves writing scripts or programs in a shell language, which is a command-line interface used in Linux and Unix operating systems. Shell scripts are used to automate tasks, execute commands, and manipulate system resources through a set of commands that are executed sequentially.

The command-line interface (CLI) and interpreter for the collection of commands used to interact with the operating system are known as the shell.

Shell scripting is powerful because it can be used to automate repetitive tasks, saving time and effort, and reducing the possibility of errors. Shell scripts can be used to manage and configure operating system settings, start or stop services, and perform file management tasks.

The shell language used in shell scripting includes different types of shells, such as bash, sh, csh, and ksh. Each of these shells has its own set of commands and syntax, although they share some common functionality.

Some common features of shell scripting include variables, loops, conditionals, functions, and I/O redirection. Variables are used to store values, such as file names or system settings, and can be manipulated using shell commands. Loops and conditionals are used to control the flow of the script, allowing for conditional branching or repeated execution of commands. Functions allow for the reuse of code across multiple scripts, reducing code duplication and improving maintainability. I/O redirection is used to redirect input and output streams to and from files, allowing for more flexible management.

This tutorial will help you learn the basic understanding of the Linux/Unix shell scripting program to advanced concepts of shell Scripting. We’ll introduce shell scripting functions and arguments. We’ll share various ways they’re used, sample code, and best practices for writing readable and reusable functions.

Definition of shell script functions

In shell scripting, functions are blocks of code that can be reused multiple times within a script or across different scripts. A function in shell scripting is similar to a subroutine or function in other programming languages. Functions are defined with a name, a set of parameters (optional), and a block of code that performs some operation. The code inside the function can access and modify variables within the function or outside the function, depending on their scope.

Examples of how functions are used in shell scripts

Note: for most of the script in this tutorial, Ubuntu 20.04 was used.Here is an example of a simple function in shell scripting: For example, let’s say you have a script called ‘myscript.sh’ and you want to pass in a file name arguments. The script can be called like this:

Inside the script, you can access the first parameter using ‘$1’ as shown below:

The first line in the script must be present for the bash script to function independently and be able to run as a command. Characters ‘#!’ is called shebang, it is a line that instructs Linux to use a certain interpreter when running a script.

In this example. The ‘myfunction’ is defined with one argument, ‘$1’ which stands for positional parameters to pass in data in the script. The ‘echo’ keyword is used to output the strings that are being passed as arguments. It is a built-in command in the shell that outputs text.

You will have to put ‘chmod’ operation in the code. Without the chmod, the shell script command when run in Ubuntu will come up with a permission denied error.

When you run “./myscript.sh” in the Linux terminal, the function is called with the argument “John”, it then prints:

The ./ notation in the above script tells Linux to look in the directory where you are now working for the ‘./myscript.sh’ script to run. The ‘.sh’ file extension might not be necessary but it is usually used to inform your user that the script is a shell file and not a binary file.

The images below represent the snapshot of the Linux terminal where the codes are executed.

Image by the Author
Image by the Author
Image by the Author

Explanation of local vs global variables in functions

Local variables are defined inside a function and can only be accessed within that function, while global variables are defined outside a function that can be accessed and modified by any part of the script.

Here is an example of a function that uses a local variable also named ‘myscript.sh’:

In this example, the ‘myfunction’ function defines a local variable ‘myvar’ and assigns it the value ‘Hello’. The value of ‘myvar’ is then used in the ‘echo’ statement to print ‘Hello’, John!” to the console.

When you run the above ‘./myscript.sh’ script, it would output:

On the other hand, a global variable is defined outside a function and can be accessed and modified by any part of the script, including functions. Global variables are useful for storing data that needs to be shared across multiple functions of the script.

Here is an example of a function that uses a global variable:

In this example, the variable ‘myvar’ is defined outside the ‘myfunction’ function, making it a global variable. The value of ‘myvar’ is then used in the ‘echo’ statements inside the function to print “Hello, John!. It is generally a good practice to use local variables whenever possible and only use global variables when necessary.

Definition of shell script arguments

In shell scripting, arguments are values that are passed to a script or a function when it is executed. These values can be used as inputs for the script or function to perform operations or make decisions. Shell scripts can take arguments in the form of command line arguments or function arguments.

Command line arguments are values that are passed to a shell script when it is executed from the command line. They are typically used to provide inputs or options to the script. Command line arguments are passed to the scripts as variables, with the first argument stored in ‘$1’, the second argument stored in ‘$2’, and so on.

Here is an example of a shell script that takes command-line arguments:

In the example above, the function takes one command line argument and uses it to print “Hello,[argumnet]!” to the console. The argument is accessed using the variable ‘$1’.

Function arguments on the other hand, are values that are passed to a function when it is allied from within a script. They are typically used to provide inputs or options to the function. Function arguments are passed to the function as variables, with the first argument stored in ‘$1’, the second argument stored in ‘$2’, and so on.

Here is an example of a function that takes arguments:

In this example, the function took one argument and used it to print “Hello”, John!”.

Examples of how arguments are used in shell scripts

Here are a few examples of how arguments can be used in shell scripts:

A Script that takes two numbers as command line arguments and adds them:

The scripts above take two numbers as command line arguments and store their sum in the variable ‘sum’. The result is then printed.

If you run this script with the command:

It will output :

Image by the Author
Image by the Author

A script that takes a list of numbers as a command line argument and calculates their average:

In this example, the script uses a ‘for’ loop to iterate over the command line arguments and add them to a running sum. It also keeps track of how many arguments were passed in the variable ‘count’. It eventually calculates the average of the numbers and prints it to the console.

If you run this script with the command:

It will output :

Image by the Author
Image by the Author
Image by the Author

Explanation of positional parameters vs named parameters

In shell scripting, two types of parameters can be passed to a script or function: positional parameters and named parameters (also called named arguments). Positional parameters are parameters that are passed to a script or function based on their position in the command line or function call. The first positional parameter is represented by ‘$1’, the second by ’$2’, and so on. Positional parameters are accessed in order, so ‘$1’ refers to the first parameter, ‘$2’ to the second parameter, and so on.

If this script is called with the command “./myscript.sh foo bar”, it will output:

Named Parameters are parameters that are passed to a function by name, rather than by position. Named parameters are accessed using their name, rather than a numerical index.

In shell scripting, named parameters are usually implemented using the ‘getopts’ command or by using associative arrays.

Example of a function that takes named parameters:

In this example, the myfunction’ function takes two named parameters: ‘-f’ for the first name and ‘-1’ for the last name. These parameters are accessed using the ‘getopts’ command and stored in the variables ‘first_name’ and ‘last_name’. When the function is called with the parameters ‘-f Steve -1 John’, it prints “Hello, Steve, John!”.

Guidelines for choosing function names and argument

When writing shell scripts and functions, choosing good names for your functions and arguments is important for making your code readable, and maintainable.

Some guidelines for choosing function names and argument names in shell scripting:

Function Names:

1. Use descriptive names that describe accurately what the function does.

2. Use verbs to describe the action performed by the function (e.g. ‘calculate_average’, ‘print_results’, ‘validate_input’).

3. Keep function names concise and easy to read

4. Avoid using abbreviations or acronyms that may not be immediately understandable to others.

Arguments Name:

1. Use descriptive names that accurately describe what the argument represents.

2. Use nouns to describe the object or value represented by the arguments (e.g. ‘filename’, ‘username’, ‘number_of_files’).

3. Keep argument names concise and easy to read.

4. Avoid using abbreviations or acronyms that may not be immediately understandable to others.

5. Use plural names for arguments that represent multiple values (e.g. ‘files’, ‘users’).

Strategies for writing modular and reusable functions

When writing shell scripts, it is important to write functions that are modular and reusable. Strategies to achieve this are below:

1. Write functions that perform a single, well-defined task: A function should do one thing, and do it well. This makes the functions more focused and easier to understand and reuse.

2. Avoid hardcoding values: Use function arguments to pass in values that may vary, such as file names or directory paths. This makes the function more flexible and reusable.

3. Return values instead of printing them: Functions should return values, rather than printing them to the console. This allows the calling code to decide how to use the function’s output, making the function more flexible and reusable.

4. Write well-documented functions: Document your functions with clear and concise comments that explain what the functions do, what their arguments are, and what their return value is.

5. Logically organize your functions: Group related functions together in your script, and organize them in a way that makes sense for the tasks you are performing.

Using function libraries in shell scripts

Here are some steps to use function libraries in your shell scripts:

1. Create a function library file: Create a separate file that contains the functions you want to reuse. This file should contain only function definitions and no code that runs outside of a function.

2. Source the function library file: In each script that uses the function library, including the line ‘source /path/to/function_library.sh’ at the beginning of the script. This will load the functions from the library file into the script.

3. Call the functions from the library: In your scripts, you can call the functions from the library file just as you would any other function. For example, if your library file contains a function called ‘calculate_average’, you could called it in your script by running ‘calculate_average’.

Example of how to create and use a function library file:

A function library called ‘function_library.sh’ is created that contains two functions, ‘print_greeting’ and ‘calculate_sqaure’. We then sourced this file in our ‘main_scripts.sh’ file and called both functions from the library in our script.

Error handling in functions

Error handling is an important part of writing functions in shell scripts. Good error-handling practices can help make your functions more reliable and robust and can make your code easier to debug.

Best Practices for error handling in functions:

  1. Return a status code to indicate success or failure. When your function completes, it should return a status code.

2. Use the ‘set -e’ option to exit on errors.

3. Use the ‘trap’ command to handle errors gracefully.

4. Print error messages to the console.

5. Validate function arguments.

6. Use descriptive variables names

Applications of Shell Scripting

  1. System administration: Shell scripts are commonly used in system administration tasks, such as automating backups, monitoring system performance, and managing user accounts.

2. Data processing: Shell scripts can be used to automate data processing tasks, such as sorting, filtering, and transforming data.

3. Web development: Shell scripts can be used to automate tasks related to web development, such as deploying web applications and managing servers.

4. Network management: Shell scripts can be used to automate tasks related to network management, such as configuring network devices, monitoring network traffic, and managing firewalls.

5. Testing and automation: Shell scripts can be used to automate testing tasks, such as running test suites and generating test reports.

6. Scientific computing: Shell scripts can be used in scientific computing, such as automating data analysis tasks and running simulations.

7. Application deployment: Shell scripts can be used to automate application deployment tasks, such as setting up environments, installing dependencies, and configuring applications.

Overall, shell scripting provides a flexible and powerful toolset for automating tasks and managing complex systems, making it an essential skill for system administrators, developers, and IT professionals.

Conclusion

Shell scripting is a powerful tool for Linux and Unix system administrators, developers, and power users, allowing for efficient management and automation of system tasks. It is a key skill for anyone working in these environments, and there are many resources available for learning shell scripting, including online tutorials, books and courses.

References :

--

--