Skip to content

Files

Latest commit

 

History

History
26 lines (17 loc) · 695 Bytes

SC2005.md

File metadata and controls

26 lines (17 loc) · 695 Bytes

Pattern: Unnecessary echo

Issue: -

Description

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.

Example of incorrect code:

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

Example of correct code:

cat 1.txt # In bash, but faster and still sticks exactly one newline: printf '%s\n' "$(<1.txt)"
# The original `echo` sticks a newline; we want it too.
< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c6; echo

Further Reading