Functions in Shell Scripting
Functions are reusable blocks of commands that perform a specific task. Instead of writing the same commands repeatedly throughout a script, you can group them into a function and execute that function whenever needed.
Functions make shell scripts more organized, readable, and easier to maintain. They are especially useful when building automation scripts that perform multiple operations.
Defining a Function
In Bash, you can define a function in two ways.
Syntax 1: Using the function Keyword
function function_name {
# Commands to execute
}Syntax 2: Without the function Keyword
function_name() {
# Commands to execute
}Both syntaxes work in Bash and perform the same task. The second form is also compatible with POSIX shells, making it a better choice when portability is important.
Calling a Function
Defining a function does not execute its commands. You must call the function by writing its name.
function_nameUnlike many programming languages, you do not use parentheses when calling a shell function.
Basic Example
Let's create a function that displays a welcome message.
#!/bin/bash
greet() {
echo "Welcome to Telusko Learning"
}
greet
greetOutput:
Welcome to Telusko Learning
Welcome to Telusko LearningHow it works:
greet()defines a function containing anechocommand.- The first
greetcall executes the function and prints the message. - The second call executes the same function again.
The command is written only once but can be executed multiple times.
Practical Example: Checking and Creating a File
Let's create a function that accepts a filename from the user and checks whether the file already exists.
- If the file exists, display its contents.
- If the file does not exist, create a new empty file.
#!/bin/bash
file_ops() {
echo "Enter the file name:"
read fname
if [ -f "$fname" ]; then
echo "File already exists. Printing content:"
cat "$fname"
else
echo "File not found. Creating a new file."
touch "$fname"
echo "File created: $fname"
fi
}
file_opsSample Execution:
Scenario 1: File already exists
Enter the file name:
devops.txt
File already exists. Printing content:
Welcome to LinuxScenario 2: File does not exist
Enter the file name:
aliens.txt
File not found. Creating a new file.
File created: aliens.txtUnderstanding the commands:
read fnameaccepts the filename entered by the user.-f "$fname"checks whether the specified path refers to an existing regular file.cat "$fname"displays the file contents.touch "$fname"creates an empty file if it does not exist.
This example combines functions, user input, conditionals, and Linux file commands into one reusable operation.
Using Multiple Functions in One Script
A shell script can contain multiple functions, with each function responsible for a specific task.
For example, consider a script that installs and starts the Nginx web server.
#!/bin/bash
install_nginx() {
sudo apt update
sudo apt install nginx -y
}
start_nginx() {
sudo systemctl start nginx
sudo systemctl enable nginx
}
install_nginx
start_nginxUnderstanding the functions:
| Function | Responsibility |
|---|---|
install_nginx() | Updates package information and installs Nginx. |
start_nginx() | Starts Nginx and enables it to start automatically at boot. |
Each function handles a separate responsibility, making the script easier to understand and maintain.
Note: This example uses apt, which is commonly available on Ubuntu and Debian-based distributions. The script should be run on a system where these commands and required permissions are available.
Summary
- Functions group commands into reusable blocks, avoiding repeated code.
- Bash supports function definitions with or without the
functionkeyword. - The
name() { ... }syntax is portable across POSIX-compatible shells. - A function must be called by its name to execute its commands.
- A script can contain multiple functions, each responsible for a different task.
- Functions can combine user input, conditional statements, and Linux commands to build practical automation scripts.
Written By: Muskan Garg
How is this guide?
Last updated on
