echo
SH-2005Consider using cmd
instead of echo $(cmd)
.
echo "$(cat 1.txt)"
echo `< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c6`
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)" ```
< /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
.
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.