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

Bash Scripting Best Practices Every Beginner Should Know

Introduction Bash scripting is a powerful way to automate Linux tasks, but writing a script…

49 minutes ago

How To Create A Self-Signed SSL Certificate Using Bash And OpenSSL

Introduction A self-signed SSL certificate is a certificate that is created and signed by the…

2 hours ago

How To Debug Bash Scripts Using bash -x And set Commands

Introduction Debugging is an important part of Bash scripting. When a script does not work…

6 hours ago

How To Use Cron Jobs With Bash Scripts For Automation

Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…

7 hours ago

How To Use Pipes In Bash Scripts For Command Chaining

Introduction Pipes are an important feature in Linux and Bash scripting. A pipe allows you…

8 hours ago

How To Use grep, awk, And sed In Bash Scripts

Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…

9 hours ago