Script Arguments
Core Idea
Arguments let one script handle different inputs.
Inside a shell script:
$1is the first argument.$2is the second argument.$#is the number of arguments."$@"means all arguments, preserving each one separately.
Practice Alone
Create a scratch script:
mkdir -p ~/scripts
micro ~/scripts/args.sh
#!/bin/bash
set -euo pipefail
printf 'count=%s\n' "$#"
printf 'first=%s\n' "${1:-missing}"
Run it with different inputs:
bash ~/scripts/args.sh
bash ~/scripts/args.sh makers
bash ~/scripts/args.sh "two words"
Watch Out
$1is empty when no first argument exists unless strict mode turns that into an error.- Use a guard before relying on required arguments.
- Quote positional parameters:
"$1", not$1.
Done When
You can write a script that behaves differently based on its arguments.
Docs Pointers
- Read bash, printf, quoting, and shell scripting.

Linux Foundations