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

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 days ago

git cherry-pick Command: Apply Commits from Another Branch

Sometimes the change you need already exists, just on the wrong branch. A hotfix lands…

2 days ago

Best Email APIs for Secure Business Email: Why Developers Are Moving Beyond SMTP

Email is still one of the most important communication channels inside modern applications. Password resets,…

3 days ago

Nginx Commands in Linux: Start, Stop, Reload, Test, and Log

Nginx is a high-performance web server and reverse proxy trusted by some of the largest…

6 days ago

ufw Command in Linux: Manage Firewall Rules with Examples

ufw (Uncomplicated Firewall) sits on top of iptables (or nftables on newer systems) and replaces…

6 days ago

who Command in Linux: Show All Logged-In Users and Sessions

When you share a server with a team or investigate unexpected activity, the first question…

6 days ago