Script Arguments

Hard-coded scripts are limited. Arguments make them flexible and reusable.

Basic Arguments

Terminal
$./script.sh arg1 arg2 arg3

Inside the script:

hljs bash
#!/bin/bash

echo "First argument: $1"
echo "Second argument: $2"
echo "Third argument: $3"
Terminal
$./script.sh hello world test
First argument: hello Second argument: world Third argument: test

Special Variables

VariableContains
$0Script name
$1 - $9Arguments 1-9
${10}Argument 10+ (need braces)
$#Number of arguments
$@All arguments (as separate words)
$*All arguments (as single string)
hljs bash
#!/bin/bash

echo "Script name: $0"
echo "Argument count: $#"
echo "All arguments: $@"
Terminal
$./info.sh one two three
Script name: ./info.sh Argument count: 3 All arguments: one two three

Check Argument Count

hljs bash
#!/bin/bash

if [[ $# -lt 2 ]]; then
    echo "Usage: $0 <source> <destination>"
    exit 1
fi

source="$1"
dest="$2"

echo "Copying $source to $dest"
Terminal
$./copy.sh
Usage: ./copy.sh <source> <destination>
$./copy.sh file.txt backup/
Copying file.txt to backup/

$@ vs $*

Terminal
$# With $@
$for arg in "$@"; do echo "[$arg]"; done
[arg one] [arg two]
$
$# With $*
$for arg in "$*"; do echo "[$arg]"; done
[arg one arg two]

"$@" preserves argument boundaries. Use this one.

Loop Through Arguments

hljs bash
#!/bin/bash
# Process all files passed as arguments

for file in "$@"; do
    echo "Processing: $file"
    wc -l "$file"
done
Terminal
$./count-lines.sh file1.txt file2.txt file3.txt
Processing: file1.txt 10 file1.txt Processing: file2.txt 25 file2.txt

Shift Arguments

shift removes the first argument, shifting others down:

hljs bash
#!/bin/bash

echo "Before shift: $1 $2 $3"
shift
echo "After shift: $1 $2 $3"
Terminal
$./shift.sh a b c
Before shift: a b c After shift: b c

Useful for processing flags:

hljs bash
#!/bin/bash

while [[ $# -gt 0 ]]; do
    case "$1" in
        -v|--verbose)
            VERBOSE=true
            shift
            ;;
        -o|--output)
            OUTPUT="$2"
            shift 2
            ;;
        *)
            FILES+=("$1")
            shift
            ;;
    esac
done

echo "Verbose: $VERBOSE"
echo "Output: $OUTPUT"
echo "Files: ${FILES[@]}"

Default Values

hljs bash
#!/bin/bash

# Use default if not provided
name="${1:-World}"
echo "Hello, $name!"
Terminal
$./greet.sh
Hello, World!
$./greet.sh Alice
Hello, Alice!

Practical Script Example

hljs bash
#!/bin/bash
# backup.sh - Flexible backup script

usage() {
    echo "Usage: $0 [-c] [-o output_dir] source_dir"
    echo "  -c              Compress backup"
    echo "  -o output_dir   Destination (default: /backup)"
    exit 1
}

# Defaults
COMPRESS=false
OUTPUT="/backup"

# Parse arguments
while [[ $# -gt 0 ]]; do
    case "$1" in
        -c)
            COMPRESS=true
            shift
            ;;
        -o)
            OUTPUT="$2"
            shift 2
            ;;
        -h|--help)
            usage
            ;;
        *)
            SOURCE="$1"
            shift
            ;;
    esac
done

# Validate
if [[ -z "$SOURCE" ]]; then
    echo "Error: Source directory required"
    usage
fi

# Run backup
echo "Backing up $SOURCE to $OUTPUT"
if $COMPRESS; then
    tar -czf "$OUTPUT/backup.tar.gz" "$SOURCE"
else
    cp -r "$SOURCE" "$OUTPUT"
fi

echo "Done!"
Terminal
$./backup.sh /var/www
Backing up /var/www to /backup Done!
$./backup.sh -c -o /tmp /var/www
Backing up /var/www to /tmp Done!
Knowledge Check

What does $# contain?

Quick Reference

VariableMeaning
$0Script name
$1-$9Positional arguments
$#Argument count
$@All args (preserved)
$*All args (single string)
${var:-default}Default value
shiftRemove first argument

Key Takeaways

  • $1, $2, etc. access positional arguments
  • $# gives argument count - use for validation
  • "$@" to loop through all arguments (preserves spacing)
  • shift moves arguments down (for parsing flags)
  • ${1:-default} provides default values
  • Always validate arguments before using them

Congratulations! You've completed Chapter 12: Shell Scripting Basics.

Next chapter: Advanced Scripting - functions, error handling, and more.