03. Passing Arguments
Overview¶
When executing a Bash script, you can pass arguments directly from the command line. These arguments can be accessed within the script using predefined positional parameters and special variables.
Basic Syntax¶
-
Each argument separated by a space is treated as a new parameter.
-
Inside the script, they can be accessed as:
-
$1→ First argument -
$2→ Second argument -
$3→ Third argument -
… and so on.
-
Example Script — arguments.sh¶
#!/bin/bash
# 03. Passing Arguments
# Access arguments directly using positional variables
echo $1 $2 $3 ' -> echo $1 $2 $3'
# Store all arguments in an array
args=("$@")
echo ${args[0]} ${args[1]} ${args[2]} ' -> args=("$@"); echo ${args[0]} ${args[1]} ${args[2]}'
# Print all arguments at once
echo $@ ' -> echo $@'
# Print number of arguments passed
echo Number of arguments passed: $# ' -> echo Number of arguments passed: $#'
Running the Script¶
Execute the script and pass three arguments:
Output¶
Bash Scripting Tutorial -> echo $1 $2 $3
Bash Scripting Tutorial -> args=("$@"); echo ${args[0]} ${args[1]} ${args[2]}
Bash Scripting Tutorial -> echo $@
Number of arguments passed: 3 -> echo Number of arguments passed: $#
Explanation¶
| Command / Variable | Description | Example Output |
|---|---|---|
$1, $2, $3 |
Accesses positional arguments (first, second, third) | Bash Scripting Tutorial |
args=("$@") |
Stores all arguments into an array | args[0] = Bash |
${args[index]} |
Access argument by array index | ${args[1]} → Scripting |
$@ |
Expands all arguments as a single string | Bash Scripting Tutorial |
$# |
Prints the number of arguments passed | 3 |
Important Notes¶
-
$0refers to the script name itself. -
Arguments beyond
$9should be accessed using curly braces, e.g.${10},${11}, etc. -
"$@"and"$*"behave differently:-
"$@"treats each argument as a separate quoted string (preferred). -
"$*"treats all arguments as a single string.
-
Example difference:¶
→ Prints each argument separately.
→ Prints all arguments as one combined string.
Bonus Tip: Argument Validation¶
You can ensure required arguments are provided:
#!/bin/bash
if [ $# -lt 2 ]; then
echo "Usage: $0 <arg1> <arg2>"
exit 1
fi
echo "Arguments received: $1 and $2"
Output when missing arguments:
Summary¶
| Feature | Variable / Command | Purpose |
|---|---|---|
| Access single argument | $1, $2, $3 |
Gets specific argument |
| Access all arguments | $@ |
Lists all arguments |
| Store arguments | args=("$@") |
Stores arguments in an array |
| Count arguments | $# |
Shows number of arguments |
| Script name | $0 |
Name of the running script |
Example in One Go¶
$ ./arguments.sh Hello Cyber Security
Hello Cyber Security -> echo $1 $2 $3
Hello Cyber Security -> args=("$@"); echo ${args[0]} ${args[1]} ${args[2]}
Hello Cyber Security -> echo $@
Number of arguments passed: 3 -> echo Number of arguments passed: $#
In Short:
Bash scripts can take arguments just like programs. You can use positional variables, arrays, and special variables like $@ and $# to handle them efficiently.
Real-World Use Case: Automated File Backup Script¶
Let’s apply what we’ve learned in a real-world automation scenario.
Here’s a Bash script that creates a timestamped backup of a file or directory — using arguments to specify the source and destination paths.
Script: backup.sh¶
#!/bin/bash
# ===============================
# Automated Backup Script
# Usage: ./backup.sh <source> <destination>
# ===============================
# Validate input arguments
if [ $# -ne 2 ]; then
echo "Usage: $0 <source_path> <destination_directory>"
exit 1
fi
# Store arguments in variables for readability
SOURCE=$1
DESTINATION=$2
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
BASENAME=$(basename "$SOURCE")
BACKUP_NAME="${BASENAME}_backup_${TIMESTAMP}.tar.gz"
# Create backup
tar -czf "$DESTINATION/$BACKUP_NAME" "$SOURCE"
# Display result
if [ $? -eq 0 ]; then
echo "Backup created successfully!"
echo "Source: $SOURCE"
echo "Destination: $DESTINATION/$BACKUP_NAME"
else
echo "Backup failed!"
fi
Example Execution¶
Output¶
Backup created successfully! Source: /home/user/documents Destination: /home/user/backups/documents_backup_20251112_163000.tar.gz
What’s Happening?¶
| Component | Explanation |
|---|---|
$1, $2 |
Used to get source and destination paths |
$(date +"%Y%m%d_%H%M%S") |
Generates a timestamp for uniqueness |
tar -czf |
Compresses the source folder into a .tar.gz archive |
$? |
Checks the exit status of the last command (0 = success) |
${BASENAME} |
Extracts the file/folder name from a full path |
This script demonstrates how Bash argument handling can be used to create dynamic, flexible automation tools.