Shell Scripting: Positional Arguments (1, $2) & Introduction to Control Flow
Command-line arguments allow shell scripts to receive dynamic data at execution time without requiring manual user interaction. By leveraging positional parameters ($0, $1, $2), DevOps engineers can create flexible, non-interactive scripts that integrate seamlessly into automated pipelines.
1. What Are Command-Line Arguments?
When executing a shell script, command-line arguments are inputs passed immediately following the script name on the terminal prompt. Each distinct argument is separated by whitespace.
In interactive scripts, execution halts to prompt the user for input using the read command. Command-line arguments eliminate this pause, making scripts non-interactive and ready for continuous integration (CI/CD) environments.
The Shell Execution Model
When a command is submitted in the terminal:
./jetha_ki_duniya.sh iyer bhide
The shell automatically parses the entire input line, breaks it into distinct tokens using spaces as delimiters, and maps each token to predefined positional variables inside the script environment.
2. Positional Parameter Mapping
positional variables map directly to the order of words typed on the command line:
The Breakdown
$0(Script Identifier): Refers to the script path or execution command itself (./jetha_ki_duniya.sh).$1(First Argument): The first value passed after the script name (iyer).$2(Second Argument): The second value passed after the script name (bhide).$N(N-th Argument): Subsequent parameters map sequentially to$3,$4, and so on.
3. Code Demonstration: Accessing Arguments
Below is a demonstration showing how command-line arguments are accessed inside a script instead of using interactive input prompts.
Naive Interactive Script (read approach)
#!/bin/bash
# Description: Requires manual keyboard input during runtime
echo "Enter character name:"
read NAME
echo "Entered name is: $NAME"
Modern Argument-Driven Script (jetha_ki_duniya.sh)
#!/bin/bash
# Description: Demonstrates dynamic positional parameters
# Accessing $0 (Script name), $1 (First argument), and $2 (Second argument)
echo "Script being executed: $0"
echo "The characters in Jethalal's world are: $1 and $2"
Terminal Execution & Output Walkthrough
$ chmod +x jetha_ki_duniya.sh
$./jetha_ki_duniya.sh iyer bhide
Execution Output:
Script being executed: ./jetha_ki_duniya.sh
The characters in Jethalal's world are: iyer and bhide
Note: If parameters are passed on the terminal but never referenced via
$1,$2, etc. inside the script, Bash quietly ignores them without throwing a runtime error.
4. Positional & Special Parameters Reference
5. Practical DevOps Use Case: Automated User Creation
In production environments, interactive read prompts block execution during automated tasks. Passing arguments directly simplifies task execution, such as user provisioning.
Why Arguments Excel in DevOps
- Interactive Prompting (
read): Requires a human operator present to type inputs line-by-line. - Argument Injection (
$1,$2, ...): Allows scripts to run via cron jobs, Ansible tasks, or Jenkins pipeline steps non-interactively.
Script Example: create_user.sh
#!/bin/bash
# Non-interactive user provisioning script
USER_NAME=$1
ROLE=$2
echo "Creating user account for: $USER_NAME"
echo "Assigning role: $ROLE"
# Production commands (e.g., sudo useradd -m $USER_NAME) execute here
Passing multiple parameters sequentially (./create_user.sh user1 user2 user3) enables rapid bulk processing through simple loop constructs.
6. Introduction to Conditional Logic (if-else)
Once scripts accept variable inputs via positional arguments, they need decision-making logic to validate those inputs or control the execution flow.
Real-Life Analogy
In the real world, relationships or transactions operate on explicit conditions:
"If you bring down the stars for me, then I will do this for you; else, I will not."
Conditional Logic Flow
Basic Bash Syntax Structure
if [ "$1" == "iyer" ]; then
echo "Access granted to Babita ji's house."
else
echo "Access denied."
fi
if [ condition ]: Evaluates whether an expression evaluates to true or false.then: Defines the code block to execute if the condition is true.else: Defines an alternative block if the condition is false.fi: Marks the end of theifstatement block (ifspelled backward).
7. Common Edge Cases and Pitfalls
-
Missing Arguments: If a script relies on
$1or$2and the user runs./script.shwithout arguments, those parameters evaluate to empty strings"", which can break downstream operations. -
Arguments Containing Spaces: If an argument contains spaces (e.g.,
Jetha Lal), wrap it in double quotes at the terminal:BASH./script.sh "Jetha Lal" "Atmaram Bhide"Otherwise,
Jethabecomes$1andLalbecomes$2. -
Unquoted Parameter Expansion inside Scripts: Always wrap positional parameters in double quotes when using them inside scripts (
"$1") to prevent unintended word splitting by the shell.
Summary and Key Takeaways
- Positional Arguments: Delimited by spaces, command-line arguments allow parameters to be injected directly into scripts at runtime.
$0Reserve Parameter: Stores the relative path or name used to invoke the script.- Sequential Indexing:
$1,$2,$3represent the 1st, 2nd, and 3rd arguments passed to the script. - Non-Interactive Automation: Using arguments replaces
readstatements, enabling fully automated CI/CD script execution. - Quotes Preserve Spaces: Enclose parameters with spaces inside double quotes (
"Argument One") to prevent split assignment across multiple parameter indexes. - Control Flow Transition: Argument verification leads directly into conditional execution blocks using
if-then-else-filogic.