Shell

Shell

Made by DeepSource

Unnecessary use of echo SH-2005

Anti-pattern
Major

Consider using cmd instead of echo $(cmd).

Problematic code:

echo "$(cat 1.txt)"
echo `< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c6`

Preferred code:

cat 1.txt 

In bash, the following construct can be used, which is even faster while preserving the same behavior: ```bash printf '%s\n' "$(<1.txt)" ```

The extra echo is used to add a newline that would be otherwise missing.

< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c6; echo ```

The command substitution $(foo) yields the result of command foo with trailing newlines erased, and when it is passed to echo it generally just gives the same result as foo.

The command echo "$(false)" will return true, whereas false of course returns false – beware of the ignored exit code before blindly altering scripts. If using set -e, the correct substitution of echo "$(cmd)" would be cmd || true.

Exceptions

One may want to use command substitutions plus echo to make sure there is exactly one trailing newline. The special command substitution $(<file) in bash is also un-outline-able.