Positional Parameters in Functions

Updated

October 31, 2026

Positional Parameters in Functions

Inside a function, positional parameters ($1, $2, $@, $#) reflect the function’s arguments, completely isolated from the script’s outer command-line arguments:

calculate_sum() {
    local sum=0
    for num in "$@"; do
        (( sum += num ))
    done
    echo "$sum"
}

total=$(calculate_sum 10 20 30)
echo "Total: $total" # 60