1

Using Debian 12 stable.

The following echo works as expected, ie. the hardcode label and the result of the expression are displayed in the same line

echo "unzip  version: $(unzip -v | head -1)"
# unzip  version: UnZip 6.00 of 20 April 2009, by Debian. Original by Info-ZIP.

echo "JQ version....: $(jq --version)" #JQ version....: jq-1.6

However for some utilities, their console version output seems to interfere with echo. The display is split in two lines in wrong order:

echo "dig version: $(dig -v)"
#DiG 9.18.28-1~deb12u2-Debian
#dig version:

printf "dig version%s\n" $(dig -v) #DiG 9.18.28-1~deb12u2-Debian #dig version

echo "SSMTP version: $(ssmtp -V)"

sSMTP 2.64 (Not sendmail at all)

SSMTP version:

Capture the result in a variable doesn't work either

DIG_VERSION="$(dig -v)"
# DiG 9.18.28-1~deb12u2-Debian

echo "Dig Version: --->$DIG_VERSION<---"

Dig Version: ---><---

What is the reason and hopefully there is a fix.

Polymerase
  • 275
  • 4
  • 7

1 Answers1

1

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: