How To

Bash Function Arguments And Return Values Explained

Introduction

Bash function arguments and return values are important for writing reusable and powerful Bash scripts. A function becomes more useful when you can pass values to it and get a result back. Instead of writing separate code for every task, you can create one function and use it with different inputs.

If you are learning Bash scripting, understanding function arguments and return values will help you build cleaner scripts for Linux automation, system checks, backups, cybersecurity tasks, and command-line tools.

What Are Bash Function Arguments?

Bash function arguments are values passed to a function when calling it. Inside the function, you can access these values using positional parameters.

ParameterMeaning
$1First argument
$2Second argument
$3Third argument
$@All arguments
$#Number of arguments

Basic Bash Function Argument Example

Create a script:

nano function-arguments.sh

Add the following code:

#!/bin/bashgreet_user() {    echo "Hello, $1"    echo "Welcome to $2"}greet_user "Kali User" "Bash Scripting"

Save and run it:

chmod +x function-arguments.sh./function-arguments.sh

Output:

Hello, Kali UserWelcome to Bash Scripting

Here, $1 receives Kali User, and $2 receives Bash Scripting.

Passing Multiple Arguments To A Function

You can pass many values to a Bash function.

#!/bin/bashshow_info() {    echo "Username: $1"    echo "Tool: $2"    echo "Website: $3"}show_info "admin" "Nmap" "kalilinuxtutorials.com"

This is useful when you want one function to work with different users, tools, files, or directories.

Using All Function Arguments

To print all arguments, use $@.

#!/bin/bashprint_tools() {    echo "Security tools:"    for tool in "$@"    do        echo "- $tool"    done}print_tools nmap curl netcat nikto

Output:

Security tools:- nmap- curl- netcat- nikto

Bash Function Return Values

In Bash, the return command is mainly used to return an exit status, not normal text. A return value should usually be between 0 and 255. In Linux, 0 means success, and non-zero means failure.

Example:

#!/bin/bashcheck_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 $? variable stores the return status of the last command or function.

Returning Text From A Bash Function

To return text, use echo and capture the output.

#!/bin/bashget_user() {    echo "$(whoami)"}current_user=$(get_user)echo "Current user is: $current_user"

This is the best method when you want a function to send back a string or command output.

Conclusion

Bash function arguments and return values make scripts more flexible and reusable. You can pass values using $1, $2, $@, and check function status using return and $?.

For beginners, this is an important Bash scripting skill. It helps you create better automation scripts, Linux tools, cybersecurity checks, and reusable command-line functions.

Cyber Defence

Recent Posts

What Is a Data Protection Platform and Why Your Organization Needs One

A data protection platform is a unified system that helps organizations discover, classify, monitor, and…

17 hours ago

How Swiss Privacy Rules Affect AI Companies Under GDPR

AI companies operating in or around Switzerland face a compliance challenge that most legal teams…

2 days ago

Swiss FADP vs EU GDPR: Key Differences for AI and Data Privacy

Two major data privacy laws now govern how organizations handle personal data across Europe; the…

3 days ago

Kali Linux Commands Cheat Sheet: Complete Quick Reference

This cheat sheet covers the essential Kali Linux commands every pentester and ethical hacker uses…

1 month ago

What I Wish I Knew Before Learning Malware Analysis and Reverse Engineering

When I first started learning malware analysis and reverse engineering, I thought the hardest part…

1 month ago

git fetch vs git pull: How They Work and When to Use Each

Both git fetch and git pull talk to a remote repository, but they do very different things to your…

2 months ago