How To

How To Use Command-Line Arguments In Bash Scripts

Introduction

Command-line arguments are values passed to a Bash script when you run it from the terminal. They make Bash scripts more flexible because you do not need to edit the script every time you want to change input values.

For example, instead of hardcoding a username, file name, IP address, or directory path inside the script, you can provide it while running the script. Command-line arguments are commonly used in Linux automation, backup scripts, system administration, cybersecurity tools, and log analysis scripts.

What Are Command-Line Arguments In Bash?

In Bash, command-line arguments are accessed using special variables called positional parameters.

VariableMeaning
$0Script name
$1First argument
$2Second argument
$3Third argument
$#Total number of arguments
$@All arguments

For example:

./script.sh kali linux

Here, kali is $1 and linux is $2.

Basic Command-Line Argument Example

Create a new Bash script:

nano arguments.sh

Add the following code:

#!/bin/bashecho "Script name: $0"echo "First argument: $1"echo "Second argument: $2"

Save the file and give execute permission:

chmod +x arguments.sh

Run the script with arguments:

./arguments.sh Kali Linux

Output:

Script name: ./arguments.shFirst argument: KaliSecond argument: Linux

Using Arguments In A Bash Script

Here is a practical example:

#!/bin/bashname=$1topic=$2echo "Hello, $name"echo "You are learning $topic"

Run it:

./arguments.sh "Kali User" "Bash Scripting"

Output:

Hello, Kali UserYou are learning Bash Scripting

Use quotes when an argument contains spaces.

Check Number Of Arguments

You can use $# to check how many arguments were passed.

#!/bin/bashif [[ $# -lt 2 ]]; then    echo "Usage: $0 username target"    exit 1fiusername=$1target=$2echo "Username: $username"echo "Target: $target"

Run it:

./script.sh admin 192.168.1.1

This is useful for preventing script errors when required values are missing.

Loop Through All Arguments

You can process all arguments using $@.

#!/bin/bashecho "Arguments provided:"for arg in "$@"do    echo "- $arg"done

Run it:

./script.sh nmap curl grep awk

Output:

Arguments provided:- nmap- curl- grep- awk

Cybersecurity Example: Scan A Target Using Argument

#!/bin/bashif [[ $# -ne 1 ]]; then    echo "Usage: $0 target-ip"    exit 1fitarget=$1echo "Scanning target: $target"nmap "$target"

Run it:

./scan.sh 192.168.1.1

This script accepts the target IP address from the command line.

Conclusion

Command-line arguments are an important part of Bash scripting. They allow you to pass values to a script when running it, making your scripts more dynamic and reusable.

For beginners, learning $1, $2, $#, and $@ is very useful. These variables help you build better Bash scripts for automation, file handling, backups, Linux administration, and cybersecurity tasks.

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…

11 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