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

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…

53 minutes 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…

5 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…

6 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…

7 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…

8 hours ago

How To Work With Files And Directories Using Bash Scripts

Introduction Working with files and directories is one of the most important skills in Bash…

9 hours ago