Useless `echo $(cmd)` — drop the `echo $(...)` wrapper

`echo $(cmd)` re-splits and re-globs the output for no reason. SC2005 says to run the command directly or quote the substitution.

Problem

Wrapping a command substitution in `echo $(...)` adds an unnecessary layer: the inner command already produces the output you want, and piping it through an unquoted `echo` just subjects it to word splitting and pathname expansion again, which can corrupt multi-line output or values containing glob characters. In almost every case this is a leftover from someone assuming `echo` is needed to "print" the result of a command, when the command's own stdout is already visible or the value should be used directly rather than re-echoed.

Bad

result=$(some_command)
echo $(echo "$result" | tr a-z A-Z)

Good

result=$(some_command)
echo "$result" | tr a-z A-Z

Explanation

Removing the outer `echo $(...)` wrapper avoids a redundant word-splitting/globbing pass and is both simpler and safer. If you do need to print a substitution, quote it: `echo "$(cmd)"`. Command substitutions rarely need to be re-echoed at all — just let the inner command's output flow through directly.

Related

SC2086

SC2046

SC2116

When It Matters

echo "$(cmd)" adds a subshell, buffers the entire output in memory, strips trailing newlines, and then prints it — when running cmd directly would have streamed the same bytes. For a command producing megabytes, that is a real memory and latency cost; for a command producing a trailing newline that matters, it is a correctness bug. It also breaks incremental output: a long-running command that prints progress appears to hang until it finishes.

Second Example

Note

Running the command directly also preserves its exit status, which the echo wrapper replaces with echo’s own (almost always zero).

Exceptions

The wrapper is justified when you need the newline-stripping behaviour, or when the value is being combined with other text. In those cases printf with a %s conversion states the intent more clearly than echo.

Faq

Q

Does the wrapper hide failures?

A

Yes. The pipeline status becomes echo’s status, so a failing command inside the substitution goes unnoticed, including under set -e.

Q

Why does the output lose its blank last line?

A

Command substitution removes all trailing newlines. echo then adds exactly one back, so any trailing blank lines are gone.

Q

Is there a memory limit?

A

The substitution holds the entire output in the shell’s memory, so a very large output can make the shell itself the bottleneck.