For AI agents: A markdown version of this page is available at https://docs.datadoghq.com/security/code_security/static_analysis/static_analysis_rules/bash-code-quality/double-negation-in-string-test.md. A documentation index is available at /llms.txt.
This product is not supported for your selected Datadog site. ().

Metadata

ID: bash-code-quality/double-negation-in-string-test

Language: Bash

Severity: Notice

Category: Code Style

Description

Using ! with [ -z … ], [ -n … ], or the same inside [[ … ]] is a double negative: [ ! -z x ] means the same as [ -n x ], and [ ! -n x ] the same as [ -z x ]. Prefer the direct -n or -z form for clarity. The same applies when ! is placed before the whole test, as in ! [ -z x ], which is equivalent to [ -n x ].

Non-Compliant Code Examples

#!/bin/bash
[ ! -z "$x" ]
[ ! -n "$x" ]
[[ ! -z "$x" ]]
! [ -z "$x" ]
! [[ -z "$x" ]]
! [ -n "$x" ]

Compliant Code Examples

#!/bin/bash
[ -z "$x" ]
[ -n "$x" ]
[[ -n "$x" ]]
[[ -z "$x" ]]
[ "$x" = y ]
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Security