Arguments
Chapter 17: Command Line Arguments
Section titled “Chapter 17: Command Line Arguments”Overview
Section titled “Overview”Command line arguments allow scripts to accept input from users at runtime. This chapter covers positional parameters, flags, options, and best practices for parsing command-line arguments in bash scripts.
Positional Parameters
Section titled “Positional Parameters”Basic Access
Section titled “Basic Access”#!/usr/bin/env bash# Access argumentsecho "Script: $0"echo "First: $1"echo "Second: $2"echo "Third: $3"echo "All: $@"echo "Count: $#"Accessing All Arguments
Section titled “Accessing All Arguments”#!/usr/bin/env bash# $@ - all arguments as separatefor arg in "$@"; do echo "Arg: $arg"done
# $* - all arguments as single stringecho "All: $*"Argument Parsing
Section titled “Argument Parsing”While Loop Parsing
Section titled “While Loop Parsing”#!/usr/bin/env bashVERBOSE=falseFORCE=falseOUTPUT=""
while [ $# -gt 0 ]; do case "$1" in -v|--verbose) VERBOSE=true shift ;; -f|--force) FORCE=true shift ;; -o|--output) OUTPUT="$2" shift 2 ;; -h|--help) echo "Usage: $0 [OPTIONS]" exit 0 ;; *) echo "Unknown: $1" shift ;; esacdone
echo "Verbose: $VERBOSE"echo "Force: $FORCE"echo "Output: $OUTPUT"Practical Examples
Section titled “Practical Examples”Example 1: Deployment Script
Section titled “Example 1: Deployment Script”#!/usr/bin/env bashset -euo pipefail
ENVIRONMENT=""VERSION="latest"DRY_RUN=false
show_help() { cat << EOFUsage: $0 [OPTIONS] ENVIRONMENT VERSION
Arguments: ENVIRONMENT Target environment (staging, production) VERSION Application version
Options: -h, --help Show this help -d, --dry-run Dry run modeEOF}
while [[ $# -gt 0 ]]; do case $1 in -h|--help) show_help exit 0 ;; -d|--dry-run) DRY_RUN=true shift ;; -*) echo "Unknown option: $1" exit 1 ;; *) if [ -z "$ENVIRONMENT" ]; then ENVIRONMENT="$1" elif [ "$VERSION" = "latest" ]; then VERSION="$1" fi shift ;; esacdone
if [ -z "$ENVIRONMENT" ] || [ "$VERSION" = "latest" ]; then show_help exit 1fi
echo "Deploying $VERSION to $ENVIRONMENT (dry-run: $DRY_RUN)"Example 2: Getopts
Section titled “Example 2: Getopts”#!/usr/bin/env bash# Using getopts for option parsingwhile getopts "vhf:o:" opt; do case $opt in v) VERBOSE=true ;; h) echo "Help"; exit 0 ;; f) FILE="$OPTARG" ;; o) OUTPUT="$OPTARG" ;; \?) exit 1 ;; esacdoneshift $((OPTIND -1))
echo "Verbose: ${VERBOSE:-false}"echo "File: ${FILE:-none}"echo "Output: ${OUTPUT:-none}"echo "Remaining: $@"Summary
Section titled “Summary”In this chapter, you learned about:
- ✅ Positional parameters
- ✅ $@ vs $*
- ✅ Argument parsing with while loop
- ✅ getopts for option parsing
- ✅ Help message creation
- ✅ Practical examples
Exercises
Section titled “Exercises”Level 1: Basics
Section titled “Level 1: Basics”- Create script that prints all arguments
- Parse simple flags
Level 2: Intermediate
Section titled “Level 2: Intermediate”- Create deployment script with argument parsing
- Implement help message
Level 3: Advanced
Section titled “Level 3: Advanced”- Build complete CLI with getopts
- Add argument validation
Next Steps
Section titled “Next Steps”Continue to the next chapter to learn about file operations.
Previous Chapter: Scope and Variable Visibility Next Chapter: File Operations