Why do some Linux utility programs send output for "normal" operation to STDERR instead of STDOUT?
For example, when I run the following command (in CentOS 7.3):
/usr/sbin/grub2-mkconfig -o /boot/grub2/grub.cfg 1> /dev/null ; echo $?
I see the output on the TTY even though the exit status is zero and there are no errors.
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-514.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-514.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-1ef39414718640fe9f6ec0f6d65fdc3e
Found initrd image: /boot/initramfs-0-rescue-1ef39414718640fe9f6ec0f6d65fdc3e.img
done
0
I've noticed a handful of other programs that do this as well. Wondering if there is some good explanation.
For reference, I need to call this this line in an RPM %post script and I'd like to redirect "normal" output to /dev/null
and capture only true "error" type messages. This particular command does not offer a -q quiet
switch.
Best Answer
grub2-mkconfig
normally writes the generated config file to stdout so you can redirect it conveniently. Status information is written to stderr since it would interfere with the redirected output. When you use the-o
argument instead, the latter behavior stays the same, which it should for sake of consistency: