Skip to content

Latest commit

 

History

History
47 lines (31 loc) · 1.07 KB

SC2120.md

File metadata and controls

47 lines (31 loc) · 1.07 KB

Pattern: Function references argument when none are passed

Issue: -

Description

In a function, $1 and up refers to the function's parameters, not the script's parameters.

If you want to process your script's parameters in a function, you have to explicitly pass them. You can do this with somefunction "$@".

Note that "$@" refers to the current context's positional parameters, so if you call a function from a function, you have to pass in "$@" to both of them:

first() { second "$@"; }
second() { echo "The first script parameter is: $1"; }
first "$@"

Example of incorrect code:

sayhello() {
  echo "Hello $1"
}
sayhello

./somescript World just prints "Hello " instead of "Hello World".

Example of correct code:

sayhello() {
  echo "Hello $1"
}
sayhello "$@"

./somescript World now prints "Hello World".

Exceptions

If the parameters are optional and you currently just don't want to use them, you can ignore this message.

Further Reading