Positional Parameters
Positional parameters are the arguments supplied to a script or function. Bash exposes individual arguments as $1, $2, and so on, the count as $#, and the complete argument list as the special parameter $@.
This lesson develops Positional Parameters from its underlying shell behavior through practical, runnable examples. The goal is not merely to memorize punctuation, but to understand what Bash expands, what it executes, and how to keep data boundaries intact.
How Positional Parameters Works
Each argument has a boundary established by the calling shell. The expansion "$@" preserves those boundaries, producing one argument for every original input. By contrast, unquoted $@ can split values again and "$*" joins everything into one string.
Bash processes source in stages: it parses the command, performs the relevant expansions, applies redirections, and finally runs a builtin, function, or external program. Quoting affects the expansion stage, while control structures react to exit statuses. Keeping these stages separate in your mental model makes debugging much faster.
Syntax
script_name=$0
first=${1:-}
count=$#
for arg in "$@"; do ...; done
Whitespace and quoting are part of the language. Do not copy a compact example and then remove quotes or spaces casually: those characters often determine argument boundaries or separate reserved words. Use bash -n script.sh to check syntax without executing the script.
Worked Examples
Example 1: Core behavior
set -- 'monthly report.csv' verbose
printf 'Count: %d\n' "$#"
printf 'File: %s\nMode: %s\n' "$1" "$2"
Output:
Count: 2
File: monthly report.csv
Mode: verbose
The first example keeps every expansion explicit and quotes values at the point where Bash builds the command’s arguments. Trace it one line at a time: assignments update shell state, expansions produce values, and printf receives a format plus distinct arguments.
Example 2: Practical composition
set -- alpha 'two words' gamma
index=0
for argument in "$@"; do
((index += 1))
printf '%d: <%s>\n' "$index" "$argument"
done
Output:
1: <alpha>
2: <two words>
3: <gamma>
The second example composes the feature with another shell capability. Its output is deterministic, so you can paste the script into a temporary file, run it, and compare every line. If your result differs, enable a trace locally and inspect the expanded arguments rather than guessing.
Execution Step by Step
- Bash reads enough input to form a complete command or compound command.
- Parameter, command, and arithmetic expansions are performed where the syntax requests them.
- Quoting determines whether expanded text stays together as one argument.
- Redirections are connected before the selected command runs.
- The command returns an exit status, which a conditional, loop, or following command may use.
This sequence also explains why printing a variable can look correct even when passing it unquoted is unsafe: the destructive splitting happens while Bash constructs arguments, not while the value is stored.
Common Mistakes
- Using $10 when ${10} is required; $10 otherwise means $1 followed by zero.
- Looping over unquoted $@ and destroying arguments containing whitespace.
Another common mistake is testing only the happy path. Shell programs interact with files, permissions, processes, and user-controlled strings. Always try an empty value, a value containing spaces, and a failing command. Those tests expose most quoting and error-handling defects early.
Best Practices
- Validate the argument count before reading required positions.
- Use "$@" to forward arguments.
- Show a concise usage message for invalid input.
For production automation, start scripts with a clear interpreter line, document inputs and outputs, keep functions small, and run ShellCheck. Strict settings can help, but explicit validation and purposeful error messages remain necessary.
Practice Exercises
- Recreate the first Positional Parameters example without copying it, then change one input containing a space.
- Add one validation failure that prints a useful message to standard error and returns a non-zero status.
- Wrap the central behavior in a function and call it twice with different arguments.
Summary
- Positional Parameters is easiest to use correctly when you track parsing, expansion, execution, and exit status separately.
- Quoted expansions preserve data boundaries and should be the default.
- Deterministic examples, syntax checks, and failure-path tests turn shell snippets into reliable automation.
