Skip to content
Emanuele Torre edited this page Jun 1, 2022 · 3 revisions

In POSIX sh, local is undefined.

Problematic code:

myfunc() {
  local i=0
  ..
}

Correct code:

In POSIX sh, you can adopt some convention to avoid accidentally overwriting variables names, e.g. prefixing with the function name:

myfunc() {
  _myfunc_i=0
  ..
}

Rationale:

local is supported in many shells, including bash, ksh, dash, and BusyBox ash. However, strictly speaking, it's not POSIX.

Exceptions:

Since quite a lot of real world shells support this feature, you may decide to ignore the warning.

Related resources:

  • Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!

ShellCheck

Each individual ShellCheck warning has its own wiki page like SC1000. Use GitHub Wiki's "Pages" feature above to find a specific one, or see Checks.

Clone this wiki locally