From e01a16505c59aa199edfbe3b77fd24a10170b7d2 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Tue, 8 Apr 2025 11:09:44 -0700 Subject: [PATCH] Created SC3062 (markdown) --- SC3062.md | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 SC3062.md diff --git a/SC3062.md b/SC3062.md new file mode 100644 index 0000000..cdbf1d3 --- /dev/null +++ b/SC3062.md @@ -0,0 +1,42 @@ +## In POSIX sh, unary -o to check options is undefined. + +### Problematic code: + +```sh +if [ -o braceexpand ] +then + echo "Brace expansion available and enabled." +fi +``` + +### Correct code: + +`$-` will be set to a list of shell options: + +```sh +case $- of + *B*) echo "Brace expansion available and enabled." +esac +``` + +However, not all options are available through `$-`. If you need to check those, ignore this suggestion with a directive: + +```sh +# shellcheck disable=SC3062 +if [ -n "$BASH_VERSION" ] && [ -o pipefail ] +then + echo "This is bash and pipefail is enabled." +fi +``` + +### Rationale: + +`[ -o option ]` is a bash/ksh extension, while `$-` is standard POSIX. Do note that letters outside the POSIX set are not guaranteed to be compatible, such as `B` above. + +### Exceptions: + +As described. + +### Related resources: + +* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc! \ No newline at end of file