Prefer dollar parens over backticks for command substitution

This product is not supported for your selected Datadog site. ().

Metadata

ID: bash-code-quality/no-old-style-command-substitution

Language: Bash

Severity: Notice

Category: Best Practices

Description

This rule flags the use of old-style command substitution (`command`) and encourages the use of the alternative form ($(command) instead).

Backtick command substitution has several drawbacks: it has undefined behavior with quoting in POSIX, uses a custom escaping mode that can produce surprising results, and is difficult to nest (e.g. nesting backticks requires escaping inner backticks). The $(...) form avoids these issues, is specified by POSIX, and is supported by all common shells (bash, ksh, etc.). It also makes nested command substitution straightforward, as in $(outer $(inner)).

To comply with this rule, replace every backtick command substitution with the equivalent $(…) form. For example, write echo "You are running on $(uname)" instead of echo "You are running on \uname`", and dir=$(dirname “$file”) instead of `` dir=dirname “$file”``. The behavior is the same; only the syntax changes. Tools like shfmt can convert backticks to$(…)` automatically.

Non-Compliant Code Examples

#!/bin/bash
echo "You are running on `uname`"

Compliant Code Examples

#!/bin/bash
echo "You are running on $(uname)"
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