-
Notifications
You must be signed in to change notification settings - Fork 1.8k
SC3043
Lucas Larson edited this page Feb 2, 2022
·
3 revisions
myfunc() {
local i=0
..
}
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
..
}
You can also unset
the variable at the end of the function:1
myfunc() {
i=0
..
unset i
}
You can also use POSIX’s set +a
or set +o allexport
which prevents the
“export attribute” from being “set for each variable to which an
assignment is performed”:2
myfunc() {
set +a || set +o allexport # either will work – don’t use both.
i=0
..
set -a || set -o allexport # optionally undo `set` modifications
}
local
is supported in many shells, including bash, ksh, dash, and BusyBox ash. However, strictly speaking, it's not POSIX.
Since quite a lot of real world shells support this feature, you may decide to ignore the warning.
- Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!