diff --git a/SC3043.md b/SC3043.md index 6882915..5ab5e8a 100644 --- a/SC3043.md +++ b/SC3043.md @@ -20,6 +20,29 @@ myfunc() { } ``` +You can also `unset` the variable at the end of the function:1 + +```sh +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 + +```sh +myfunc() { + set +a || set +o allexport # either will work – don’t use both. + i=0 + .. + set -a || set -o allexport # optionally undo `set` modifications +} +``` + ### Rationale: `local` is supported in many shells, including bash, ksh, dash, and BusyBox ash. However, strictly speaking, it's not POSIX. @@ -29,5 +52,8 @@ myfunc() { Since quite a lot of real world shells support this feature, you may decide to [[ignore]] the warning. ### Related resources: +1. [Shell Command Language § `unset`](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset) ([archived](https://web.archive.org/web/20180326004734/https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset)) +2. [Shell Command Language § Description](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_25_03) ([archived](https://web.archive.org/web/20180326004734/https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_25_03)) + * Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!