From 6ae77c59ce1f4e3e6c2c93b3badb00ea513148e7 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Sun, 30 Jul 2023 19:39:58 -0700 Subject: [PATCH] Created SC2325 (markdown) --- SC2325.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 SC2325.md diff --git a/SC2325.md b/SC2325.md new file mode 100644 index 0000000..05750c3 --- /dev/null +++ b/SC2325.md @@ -0,0 +1,34 @@ +## Multiple ! in front of pipelines are a bash/ksh extension. Use only 0 or 1. + +### Problematic code: + +```sh +#!/bin/sh +! ! true +``` + +### Correct code: + +```sh +#!/bin/sh +true +``` +### Rationale: + +POSIX (and Dash) does not allow multiple `!` pipeline negations in a row. It's also logically unnecessary. + +Use either zero or one `!`. + +### Exceptions: + +Scripts whose shebang declares it will run with Ksh and Bash will not trigger this warning. + +If you really want to negate multiple times on POSIX or Dash, e.g. to normalize exit codes to 0 or 1, use `cmd || false` or a command group: + +```sh +! { ! true; } +``` + +### Related resources: + +* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc! \ No newline at end of file