Skip to content

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:

function function_name {
    commands
}

or:

function_name() {
    commands
}

The function is executed by writing its name:

function_name

Basic Function Example

#!/bin/bash

function greet {
    echo "Hello, World!"
}

greet

Output

Hello, World!

The function greet is declared first and then called using:

greet

Functions With Arguments

Bash functions can accept arguments in the same way that scripts accept command-line arguments.

Inside a function:

$1

represents the first argument passed to the function.

$2

represents the second argument.

$3

represents the third argument.

Example

#!/bin/bash

function greet {
    echo "Hello, $1"
}

greet "Subrat"

Output

Hello, Subrat

Here:

greet "Subrat"

passes Subrat as the first argument to the function.

Inside the function, $1 contains:

Subrat

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

Username: subrat
Role: Security Analyst

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

Function A.
Function B.
Function C.
Function D.

In this example:

function_A "Function A."

calls function_A and passes Function A. as its first argument.

Inside function_A:

echo "$1"

prints the first argument.

The following function does not require an argument:

function_B

because it simply prints a predefined message.

Similarly:

function_C "Function C."

passes an argument to function_C.

Finally:

function_D

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

Number of arguments: 3
Arguments: Nmap Burp Suite Wireshark

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:

0 = success
non-zero = failure

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:

current_host

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

Checking target: 192.168.1.10
Starting port scan against: 192.168.1.10
Scan completed.

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.

  • return can 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.