---
title: Avoid double negation in string test
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: >-
  Docs > Datadog Security > Code Security > Static Code Analysis (SAST) > SAST
  Rules > Avoid double negation in string test
---

# Avoid double negation in string test

{% callout %}
# Important note for users on the following Datadog sites: app.ddog-gov.com, us2.ddog-gov.com

{% alert level="danger" %}
This product is not supported for your selected [Datadog site](https://docs.datadoghq.com/getting_started/site.md). ().
{% /alert %}

{% /callout %}

## Metadata{% #metadata %}

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

**Language:** Bash

**Severity:** Notice

**Category:** Code Style

## Description{% #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{% #non-compliant-code-examples %}

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

## Compliant Code Examples{% #compliant-code-examples %}

```bash
#!/bin/bash
[ -z "$x" ]
[ -n "$x" ]
[[ -n "$x" ]]
[[ -z "$x" ]]
[ "$x" = y ]
```
  Seamless integrations. Try Datadog Code SecurityDatadog Code Security 
{% icon name="icon-external-link" /%}
 