From e9da80228ae7c4b7e9b9f7e123b6e1c45a4a4834 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Tue, 1 Sep 2020 18:11:01 -0700 Subject: [PATCH] Created SC3047 (markdown) --- SC3047.md | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 SC3047.md diff --git a/SC3047.md b/SC3047.md new file mode 100644 index 0000000..f7f5878 --- /dev/null +++ b/SC3047.md @@ -0,0 +1,39 @@ +## In POSIX sh, trapping ERR is undefined. + +(or "In dash, ... is not supported." when using `dash`) + +Also applies to RETURN and DEBUG. + +### Problematic code: + +```sh +#!/bin/sh +trap 'Command failed with $?' ERR +``` + +### Correct code: + +Switch to a shell like `ksh` or `bash` that *does* support the kind of trap you want: + +```sh +#!/bin/bash +trap 'Command failed with $?' ERR +``` + +Otherwise, rewrite the script to not rely on the trap. + +### Rationale: + +You are trying to install a trap that is not supported by your current shell (`dash` or POSIX `sh`). There is no simple replacement. The script should be rewritten to avoid depending on the trap, or simply be run with a shell that supports it. + +### Exceptions: + +If you only intend to target shells that supports this feature, you can change +the shebang to a shell that guarantees support, or [[ignore]] this warning. + +You can use `# shellcheck disable=SC3000-SC4000` to ignore all such compatibility +warnings. + +### Related resources: + +* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!