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/ambiguous-compound-test-operators.md. A documentation index is available at /llms.txt.
This product is not supported for your selected Datadog site. ().

Metadata

ID: bash-code-quality/ambiguous-compound-test-operators

Language: Bash

Severity: Warning

Category: Code Style

Description

Inside [ ... ], combining expressions with -a (logical and) or -o (logical or) is easy to get wrong: POSIX marks those XSI combining operators as obsolescent and notes that many such expressions are ambiguously defined. Precedence also differs from shell && and ||.

It is better to chain separate bracket tests with shell logical operators. For example, replace [ -f file -a -r file ] with [ -f file ] && [ -r file ]. In Bash, you can also use [[ ... ]] and combine conditions with && or || instead of -a and -o.

Non-Compliant Code Examples

#!/bin/bash
[ a = b -a c = d ]
[ x -o y ]
[ x = y -o z = w ]

Compliant Code Examples

#!/bin/bash
[[ a && b ]]
[[ -a file ]]
[ -a file ]
[ -e foo ]
[ x = y ]
[ x = y ] && [ z = w ]
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