echo? Instead of echo $(cmd), just use
cmdecho "$(whoami)"whoamiShellCheck found the unnecessary construct
echo "$(somecommand here)".
This is generally due to a misunderstanding about what
echo does. It has no role in "showing on screen" or
similar, but simply writes a string to standard output. This is also how
all other programs output data.
echo "$(somecommand)" will capture the output
somecommand writes to standard output and write it to
standard output, where it was already going. At best this is a no-op,
but it may have several other negative effects:
echo "$(find . -name '*.iso')" | xargs sha1sum which does
not allow iterating files and checksumming at the same time. Similarly,
users don't see incremental updates as programs run.-n, stripping NUL bytes and trailing
linefeeds, and expanding escape sequences in some shells but not
others.echo "$(grep '^user:' /etc/passwd)" no longer returns with
failure when the user is not found.ls vs echo "$(ls)" where the former
outputs columns and colors according to user preferences, while the
latter doesn't.To avoid all this, simply replace echo "$(somecommand)"
with somecommand as in the example. It's shorter, faster,
and more correct.
If you are relying on one of the otherwise detrimental effects for correctness, you can consider one of:
# Suppress exit code without the other negative effects
cmd || true
# Disable tty specific output without the other negative effects
cmd | cat
# Buffer up potentially large output without using more memory or modifying the content in any way
cmd > file.tmp
cat file.tmp
# Exactly like `echo "$(cmd)"`, but allows output like `-n` and works the same across shells
printf '%s\n' "$(cmd)"ShellCheck is a static analysis tool for shell scripts. This page is part of its documentation.