jq --version
prints to its stdout, so $(jq --version)
manages to capture this output and everything works as you expect.
dig -v
prints to its stderr. It probably should print to stdout, because it prints what you have requested and do expect (compare this answer); still it prints to stderr.
In echo "dig version: $(dig -v)"
, at first dig -v
prints to its stderr, which usually (and certainly in your case) happens to be a terminal. Only when dig
terminates, $()
captures what dig
has printed to its stdout: nothing. Then the command becomes echo "dig version: "
and this gets executed.
You can redirect stderr to the open file description of stdout with 2>&1
:
echo "dig version: $(dig -v 2>&1)"
Related: