From 94049681aa8e3d442af12f748e36a8b76ec5e613 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Tue, 27 Jul 2021 19:46:25 -0700 Subject: [PATCH] Created SC2292 (markdown) --- SC2292.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 SC2292.md diff --git a/SC2292.md b/SC2292.md new file mode 100644 index 0000000..64daabb --- /dev/null +++ b/SC2292.md @@ -0,0 +1,31 @@ +## Prefer [[ ]] over [ ] for tests in Bash/Ksh. + +This is an [[optional]] suggestion. It must be explicitly enabled with a [[directive]] `enable=require-double-brackets` in a `# shellcheck` comment or `.shellcheckrc` + +### Problematic code: + +```sh +[ -e /etc/issue ] +``` + +### Correct code: + +```sh +[[ -e /etc/issue ]] +``` + +### Rationale: + +ShellCheck has been explicitly asked to warn about uses of `[ .. ]` in favor of the extended Bash/Ksh test `[[ .. ]]`. + +`[[ .. ]]` suppresses word splitting and globbing, supports a wider variety of tests, and is generally safer and better defined than `[ .. ]`. For an in-depth list of differences, see the Related Resources. + +### Exceptions: + +This check is not enabled by default, and may have been turned on for your current project by someone who wants it enforced. You can still [[ignore]] it with a directive. + +This suggestion does not trigger for Sh or Dash scripts, even when explicitly enabled, as these shells don't support `[[ .. ]]`. + +### Related resources: + +* BashFAQ 031: [What is the difference between test, \[ and \[\[ ?](https://mywiki.wooledge.org/BashFAQ/031)