Bash Scripting Functions — Writing Clean, Reusable Automation Code (Part 3)
Up to this point, you’ve learned many Bash scripting concepts:
Variables
Conditionals
Loops
Parameters
User input
Now we arrive at the final core concept of Bash scripting: functions.
Functions are what turn Bash scripts from long, messy command lists into clean, readable, reusable automation tools.
Why Functions Are Important
Even with simple examples, Bash scripts can quickly grow:
Multiple
ifstatementsNested loops
Repeated blocks of logic
In real DevOps scripts server setup, monitoring, backups you can easily end up with hundreds of lines of code.
This creates two major problems:
Scripts become hard to read and understand
Similar logic gets repeated in multiple places
Functions solve both problems.
What Is a Function in Bash?
A function is:
A named block of code
A reusable container for logic
A way to group related commands together
Instead of repeating the same logic multiple times, you:
Put it in a function
Call the function whenever you need it
Just like variables:
You define them once
You use them many times
Defining a Function
Basic function syntax:
function score_game() {
# logic goes here
}
Everything inside { } belongs to the function.
Indentation is not required by Bash, but it greatly improves readability.
Functions Do Not Run Automatically
Defining a function does not execute it.
Just like defining a variable does not print it.
To run a function, you must call it by name:
score_game
Example: Moving Existing Logic into a Function
If you previously had a loop or a program that:
Asked for scores
Calculated a total
Repeated until the user quit
You can move that entire logic into a function:
function score_game() {
sum=0
while true; do
read -p "Enter score (Q to quit): " score
if [ "$score" = "Q" ]; then
break
fi
sum=$((sum + score))
echo "Total score: $sum"
done
}
And execute it with:
score_game
Commenting Code in Bash
To temporarily disable or explain code, use comments.
# This line will not be executed
Comments are useful for:
Documentation
Debugging
Explaining logic
Temporarily disabling commands
Functions with Parameters
Functions can accept parameters just like scripts.
Inside a function:
$1→ first parameter$2→ second parameter
Example: A reusable file-creation function
function create_file() {
filename="$1"
touch "$filename"
echo "File $filename created"
}
Calling the function:
create_file test.txt
create_file myfile.yaml
create_file myscript.sh
Best Practice: Keep Functions Small
A good guideline:
More than 5 parameters → function is probably doing too much
Split large logic into smaller functions
Functions should:
Do one thing
Do it well
Be easy to understand
Using Boolean Parameters
Functions can accept Boolean values (true / false).
Example:
function create_file() {
filename="$1"
is_shell_script="$2"
touch "$filename"
if [ "$is_shell_script" = true ]; then
chmod u+x "$filename"
echo "Added execute permission"
fi
}
Calling it:
create_file test.txt false
create_file myscript.sh true
true and false are Bash keywords representing Boolean values.
Returning Values from Functions
Functions can return a result.
Example: Summing two numbers
function sum_numbers() {
result=$(($1 + $2))
return $result
}
Calling the function:
sum_numbers 2 10
result=$?
echo "Sum of 2 and 10 is $result"
$? — Special Return Variable
$?holds the return value of the last executed commandCommonly used to capture function results or command exit codes
How Functions Fit into Bigger Scripts
Functions can be:
Used inside loops
Called inside conditionals
Combined with parameters
Reused across scripts
They help structure scripts that:
Configure servers
Install software
Create users
Monitor systems
Perform backups
Interact with external APIs
Bash Scripting in the Real World
Bash scripting is:
Powerful
Portable
Widely available
But also:
Verbose
Syntax-heavy
Harder to maintain at large scale
That’s why DevOps engineers often combine:
Bash (quick automation)
Python (complex logic)
Configuration tools (Ansible, etc.)
Knowing Bash gives you the foundation to understand and debug all of them.
Final Takeaway
In this final Bash scripting lecture, you learned:
What functions are
Why they matter
How to define and call functions
How to pass parameters
How to use Boolean values
How to return values
How functions improve readability and reuse
With variables, conditionals, loops, parameters, environment variables, and functions, you now know the core building blocks of Bash scripting.
Bash scripts are:
Automation tools
Documentation
Reusable DevOps assets
