From c9bddfd540a4786829ade21cc03c5ef19f64a7e3 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Wed, 2 Nov 2022 20:15:19 -0700 Subject: [PATCH] Created SC2265 (markdown) --- SC2265.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 SC2265.md diff --git a/SC2265.md b/SC2265.md new file mode 100644 index 0000000..3d64340 --- /dev/null +++ b/SC2265.md @@ -0,0 +1,30 @@ +## Use && for logical AND. Single & will background and return true. + +### Problematic code: + +```sh +if [ "$1" = "install" ] & [ "$USER" != "root" ] +then + echo "Must be root to install" +fi +``` + +### Correct code: + +```sh +if [ "$1" = "install" ] && [ "$USER" != "root" ] +then + echo "Must be root to install" +fi +``` +### Rationale: + +ShellCheck found a `test` command followed by a `&`. This runs the test in the background, effectively ignoring it. To specify "logical AND" between two commands, use `&&`. + +### Exceptions: + +None + +### Related resources: + +* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc! \ No newline at end of file