I learned that in order to capture the output of a Unix command one should use something like

RESULT=$(command in question)

I want to capture the version of fsarchiver to the variable RESULT because the former option -z of fsarchiver has been deprecated. One should use -Z instead for version 0.8.4 (the one used under Ubuntu 18.04 LTS). In order to apply the right compression level in my script, it has to distinguish the current version of fsarchiver.

So I tried this:

$ RESULT=$(fsarchiver --version)
fsarchiver 0.6.22 (2016-02-13)
$ echo "${RESULT}"


so this one just returns an empty line. However I can determine the version of ls this way so I am sure to not have completely misunderstood capturing the output of a command to a variable in a bash script:

$ RESULT=$(ls --version)
$ echo "${RESULT}"
ls (GNU coreutils) 8.25
Copyright © 2016 Free Software Foundation, Inc.
Lizenz GPLv3+: GNU GPL Version 3 oder höher <http://gnu.org/licenses/gpl.html>
Dies ist freie Software: Sie können sie ändern und weitergeben.
Es gibt keinerlei Garantien, soweit wie es das Gesetz erlaubt.

Geschrieben von Richard M. Stallman und David MacKenzie.

Why do ls and fsarchiver behave differently with respect to capturing their results when asking for their versions?


fsarchiver writes its version information to its standard error, ls to its standard output. Which one is right is a matter of debate; see Do progress reports/logging information belong on stderr or stdout? for some related discussion. ls in your case is likely to be the GNU version, and the GNU coding standards mandate writing version information to standard out.

You can capture the version using

RESULT=$(fsarchiver --version 2>&1)
  • Stephen, thank you for the explanation and the hint how to redirect stderr to stdout. This solves my problem. Just one question: is there a way to know if a program writes to stderr or to stdout, because just from observing what it writes to the screen, I see no difference. – Adalbert Hanßen Jan 13 at 16:37
  • Not directly, but you can use a tool such as annotate-output to see where output is being written. – Stephen Kitt Jan 13 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.