From 50cbe8f3127679709f19cd8fda018b370a6e0042 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Sat, 11 Dec 2021 16:55:40 -0800 Subject: [PATCH] Created SC2111 (markdown) --- SC2111.md | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 SC2111.md diff --git a/SC2111.md b/SC2111.md new file mode 100644 index 0000000..dc98c0e --- /dev/null +++ b/SC2111.md @@ -0,0 +1,48 @@ +## ksh does not allow 'function' keyword and '()' at the same time. + +### Problematic code: + +```sh +#!/bin/ksh +function foo() { + echo "Hello World" +} +``` + +### Correct code: + +```sh +# POSIX sh function +foo() { + echo "Hello World" +} +``` + +or + +```sh +# ksh extended function +function foo { + echo "Hello World" +} +``` + +### Rationale: + +Ksh allows two ways of defining functions: POSIX sh style `foo() { ..; }` and Ksh specific `function foo { ..; }`. + +ShellCheck found a function definition that uses both at the same time, `function foo() { ..; }` which is not allowed. Use one or the other. + +Note that the two are not identical, for example: + +* Using `typeset` in a `function foo` will create a local variable, while in `foo()` it will create a global variable. +* `function foo` has its own trap context, while `foo()` shares them with the current process. +* `function foo` will set `$0` to foo, while `foo()` will inherit `$0` from the current process. + +### Exceptions: + +In Bash, `function foo() { ..; }` is allowed, and `function foo` and `foo()` are identical. This warning does not trigger when the shebang is e.g. `#!/bin/bash`. + +### Related resources: + +* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc! \ No newline at end of file