Function uses `$1` but was called with no args
SC2119 warns when a function referencing `$1` etc. is invoked with no arguments, likely inheriting the caller's unrelated positional params.
Problem
When a function body references `$1`, `$@`, or other positional parameters but is called without any arguments, Bash does not clear or scope positional parameters per call in the way variables are scoped — the function instead sees whatever positional parameters happen to be set in the calling context (the script's own `$1`, or the caller function's arguments). This makes the function's behaviour depend on unrelated state from wherever it happens to be called, which is a common source of "works here, breaks there" bugs.
Bad
greet() {
echo "Hello, $1"
}
greet # $1 here is whatever the script's own $1 is, not emptyGood
greet() {
local name="$1"
echo "Hello, $name"
}
greet "World" # pass arguments explicitly at every call siteExplanation
Always call functions that use positional parameters with explicit arguments, and pair this with SC2120 to confirm the function does expect arguments at every call site. If a function is meant to be usable with no arguments, add an explicit default: `local name="${1:-World}"`.
Related
SC2120
SC2124
SC2034
When It Matters
A function that references $1 but is called with no arguments reads the caller’s parameters in some shells and nothing in others — either way the value is not what the author intended. The common misconception is that a function inherits the script’s positional parameters; it does not, but it also does not reset them to empty in every construct. The result is a function that appears to work when the script happens to be invoked with matching arguments, and fails when it is not.
Second Example
Note
Using ${1:?message} turns a missing argument into an immediate, self-describing error instead of an empty string.
Exceptions
A function with genuinely optional parameters should say so by defaulting them: local name=${1:-world}. Once the default is written, the call with no arguments is clearly intentional and the warning no longer fires.
Faq
Q
Do functions see the script’s positional parameters?
A
No. Inside a function, $1 and friends refer to the function’s own arguments; the script’s are shadowed for the duration of the call.
Q
How do I forward all arguments?
A
Call the function as fn "$@", which passes each argument as a separate word.
Q
What is the difference from SC2120?
A
SC2119 fires at a call site with no arguments; SC2120 fires at the function definition when no caller ever passes any.