SC2006: Replace Backtick Command Substitution with $()
Migrate from deprecated backtick `command` to modern $(command) syntax. Learn why $() is preferred for readability, nesting, and escaping.
Detailed Explanation
Backticks vs $() Command Substitution
Backtick command substitution (`command`) is the original POSIX syntax,
but $(command) is the modern, preferred form. While backticks still work,
they have several practical problems.
Why $() Is Better
1. Nesting is clean:
# With $(): readable nesting
echo "Version: $(cat $(find /opt -name version.txt -print -quit))"
# With backticks: requires escaping
echo "Version: \`cat \\\`find /opt -name version.txt -print -quit\\\`\`"
2. Consistent backslash handling:
# $() preserves backslashes as-is
echo $(echo "path\\to\\file") # Output: path\to\file
# Backticks consume one level of backslash escaping
echo \`echo "path\\\\to\\\\file"\` # Need double escaping
3. Visual clarity:
# Easy to see boundaries with $()
result=$(long_command --with-many "arguments" | filter)
# Backticks can be confused with single quotes in some fonts
result=\`long_command --with-many "arguments" | filter\`
Migration Examples
# Before (backticks)
DATE=\`date +%Y-%m-%d\`
FILES=\`find . -name "*.log"\`
COUNT=\`wc -l < "$file"\`
# After ($())
DATE=$(date +%Y-%m-%d)
FILES=$(find . -name "*.log")
COUNT=$(wc -l < "$file")
POSIX Compatibility
Both forms are defined in POSIX. $() was added in POSIX.1-2001 and
is supported by all modern shells including dash, ash, and busybox sh.
There is no portability reason to prefer backticks.
Exceptions
There are no practical cases where backticks are necessary. The only argument for backticks is compatibility with very old shells (pre-POSIX), which are extremely rare in modern environments.
Use Case
Modernizing legacy shell scripts, enforcing consistent coding standards in a team, and writing new scripts that are readable and maintainable. Particularly relevant when refactoring older automation scripts.