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

Best OSINT Tools for Journalists 2026: Verify Sources, Images and Claims

Journalists use OSINT to verify public information before publishing. In 2026, misinformation, AI-generated images, fake…

5 hours ago

Install Docker on Ubuntu 20.04: Complete Step-by-Step Guide

DockerĀ is an open-source platform that lets you package and run applications inside containers. Each container…

15 hours ago

Install PostgreSQL on Ubuntu: Database Setup and Admin Guide

PostgreSQL (often called Postgres) is an open-source relational database system. It supports advanced features like JSON…

16 hours ago

Install Xrdp Remote Desktop on Ubuntu: Setup and Connect

Xrdp is an open-source server that lets you connect to your Ubuntu machine from another computer…

16 hours ago

Tomcat 9 on Ubuntu 20.04: Install, Configure, and Start

Apache Tomcat is an open-source web server and Java servlet container. It is one of the…

17 hours ago

Automatic Updates on Ubuntu: Set Up unattended-upgrades

Keeping your Ubuntu system updated is one of the best ways to protect it. Security…

18 hours ago