Scripts read command-line arguments through $1, $2, and related parameter variables. Defaults keep a script useful when callers omit an argument.

Program

Play the no-argument run to watch Bash choose fallback values for user and mode.

arguments.sh
Replay: real traced execution (multi-file project)
#!/usr/bin/env bash

user="${1:-guest}"
mode="${2:-preview}"
echo "$user:$mode"
  1. user ← guest

    3user="${1:-guest}"4mode="${2:-preview}"
    values this stepguestuserunset$1
  2. mode ← preview

    3user="${1:-guest}"4mode="${2:-preview}"5echo "$user:$mode"
    values this steppreviewmodeunset$2
  3. echo "$user:$mode"

    4mode="${2:-preview}"5echo "$user:$mode"
    outputguest:preview
    values this stepguestuserpreviewmode

Follow the Defaults

  1. The script runs with no arguments.
  2. $1 is unset, so user="${1:-guest}" chooses guest.
  3. $2 is unset, so mode="${2:-preview}" chooses preview.
  4. The script prints guest:preview. | input slot | visible value | chosen value | | --- | --- | --- | | $1 | unset | guest | | $2 | unset | preview | | stdout | - | guest:preview |
positional parameter `$1` is the first argument passed to a script or function.
default expansion `${name:-fallback}` uses `fallback` when the variable is unset or empty.

Exercise: arguments.sh

Reproduce guest:preview, then identify which fallback value fills user and which fallback value fills mode.