12. Bash Function
Overview¶
Bash functions allow you to group a set of commands into a reusable block. Instead of writing the same commands multiple times, you can define a function once and call it whenever you need it.
Functions are commonly used to make Bash scripts more organized, reusable, and easier to maintain.
Function Syntax¶
A Bash function can be declared using either of these formats:
or:
The function is executed by writing its name:
Basic Function Example¶
Output¶
The function greet is declared first and then called using:
Functions With Arguments¶
Bash functions can accept arguments in the same way that scripts accept command-line arguments.
Inside a function:
represents the first argument passed to the function.
represents the second argument.
represents the third argument.
Example¶
Output¶
Here:
passes Subrat as the first argument to the function.
Inside the function, $1 contains:
Multiple Function Arguments¶
A function can accept multiple arguments.
#!/bin/bash
function user_info {
echo "Username: $1"
echo "Role: $2"
}
user_info "subrat" "Security Analyst"
Output¶
The first argument is stored in $1, while the second argument is stored in $2.
Declaring Multiple Functions¶
A script can contain multiple functions. Functions can be declared in different parts of the script, but they generally need to be defined before they are executed.
#!/bin/bash
function function_B {
echo "Function B."
}
function function_A {
echo "$1"
}
function function_D {
echo "Function D."
}
function function_C {
echo "$1"
}
function_A "Function A."
function_B
function_C "Function C."
function_D
Output¶
In this example:
calls function_A and passes Function A. as its first argument.
Inside function_A:
prints the first argument.
The following function does not require an argument:
because it simply prints a predefined message.
Similarly:
passes an argument to function_C.
Finally:
calls function_D.
Function Arguments¶
The commonly used function argument variables are:
| Variable | Description |
|---|---|
$1 |
First argument |
$2 |
Second argument |
$3 |
Third argument |
$# |
Number of arguments |
$@ |
All arguments |
$? |
Exit status of the last command |
Example Using $# and $@¶
#!/bin/bash
function show_args {
echo "Number of arguments: $#"
echo "Arguments: $@"
}
show_args "Nmap" "Burp Suite" "Wireshark"
Output¶
Returning a Value From a Function¶
Bash functions can return an exit status using return.
#!/bin/bash
function check_file {
if [ -f "$1" ]; then
return 0
else
return 1
fi
}
check_file "/etc/passwd"
if [ $? -eq 0 ]; then
echo "File exists."
else
echo "File does not exist."
fi
The return value is an exit status:
Using Command Output From a Function¶
If you want a function to produce a value that can be stored in a variable, use command substitution.
#!/bin/bash
function get_hostname {
hostname
}
current_host=$(get_hostname)
echo "Hostname: $current_host"
The output generated by the function is captured in:
Practical Example¶
Functions are useful for separating different tasks in a security script.
#!/bin/bash
function check_target {
echo "Checking target: $1"
}
function scan_ports {
echo "Starting port scan against: $1"
}
function show_result {
echo "Scan completed."
}
target="192.168.1.10"
check_target "$target"
scan_ports "$target"
show_result
Output¶
This approach makes the script easier to understand because each function is responsible for a specific task.
Key Points¶
-
A function is a reusable block of Bash commands.
-
Functions are called by using their function name.
-
$1,$2,$3, and so on represent function arguments. -
$#contains the number of arguments. -
$@represents all arguments. -
returncan be used to return an exit status. -
Command substitution can capture output from a function.
-
Functions help make larger Bash scripts modular and easier to maintain.