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

Install Pip on Ubuntu 18.04: Python 3 and Python 2 Setup Guide

Pip is the official package manager for Python and the standard way to install libraries from…

3 days ago

Install R on Ubuntu 18.04 from CRAN: Statistical Computing Setup

R is an open-source programming language and environment built for statistical computing and data visualization. It…

3 days ago

Install Jenkins on Ubuntu 18.04: CI/CD Server Setup Guide

Jenkins is an open-source automation server that makes it easy to build CI/CD pipelines. Continuous integration…

3 days ago

Install Android Studio on Ubuntu 18.04 with Snap and OpenJDK 8

Android Studio is the official IDE for Android development, built on JetBrains' IntelliJ IDEA platform. It…

3 days ago

Install and Configure GitLab on Ubuntu 18.04 with Omnibus

GitLab is a web-based, open-source Git repository manager written in Ruby. It includes built-in tools for…

3 days ago

Install Anaconda on Ubuntu 18.04: Python Data Science Setup Guide

Anaconda is the most widely used Python distribution for data science and machine learning. It bundles…

4 days ago