From 40f5ce0eee45f4ecc27232d838e0cb05383708f7 Mon Sep 17 00:00:00 2001
From: Lucas Larson <91468+LucasLarson@users.noreply.github.com>
Date: Wed, 2 Feb 2022 16:38:38 -0500
Subject: [PATCH] =?UTF-8?q?use=20POSIX=E2=80=99s=20`unset`=20or=20`set=20+?=
=?UTF-8?q?a`=20or=20`set=20+o=20allexport`=20instead=20of=20`local`?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
SC3043.md | 26 ++++++++++++++++++++++++++
1 file changed, 26 insertions(+)
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!