6

So if I enter the command

$ gzip --version | head -n1

everything goes as expected. But if I try the same with bzip2:

$ bzip2 --version | head -n1

I get a lot of lines and I have to press Ctrl-C to terminate.

What is happening here?

EDIT:

The lines being printed by

$ bzip2 --version | head -n1

bzip2, a block-sorting file compressor.  Version 1.0.6, 6-Sept-2010.

Copyright (C) 1996-2010 by Julian Seward.

This program is free software; you can redistribute it and/or modify
it under the terms set out in the LICENSE file, which is included
in the bzip2-1.0.6 source distribution.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
LICENSE file for more details.

and i have to press Ctrl-C to continue.

If I omit the pipe I get

$ bzip2 --version 

bzip2, a block-sorting file compressor.  Version 1.0.6, 6-Sept-2010.

Copyright (C) 1996-2010 by Julian Seward.

This program is free software; you can redistribute it and/or modify
it under the terms set out in the LICENSE file, which is included
in the bzip2-1.0.6 source distribution.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
LICENSE file for more details.

bzip2: I won't write compressed data to a terminal.
bzip2: For help, type: `bzip2 --help'.

If I merge stdout with stderr as @devnull sais, it displays the lines well, but i need to press Ctrl-C. I tried

$ gcc 2>&1 | head -n1

and it works well, so I think there is something still missing in the bzip2 command.

EDIT 2:

I solved the issue with the following command:

$ bzip2 --version 2>&1 < /dev/null | head -n1 

But I still don't understand the problem.

don_crissti
  • 79,330
  • 30
  • 216
  • 245
Msegade
  • 572
  • 1
  • 4
  • 13
  • Could you show some of the lines being printed? Does `\bzip2 --version 2>&1 | head -n1` work better? – terdon Jan 31 '14 at 15:07
  • The version # appears and the license block. If the pipe is removed, I see also: "bzip2: I won't write compressed data to a terminal." –  Jan 31 '14 at 16:41
  • @terdon It works better, but i still have to press Ctrl-C, and I don't know why. – Msegade Jan 31 '14 at 19:56
  • Please post the output of `type bzip2` and `type head`. – terdon Jan 31 '14 at 20:00
  • @terdon `type bzip2` gives `bzip2 is /usr/bin/bzip2`. `type head` gives `head is /usr/bin/head`. – Msegade Jan 31 '14 at 20:03
  • There is something going on here that is specific to your system. I can't reproduce the issue. Could you update your question with the result of `bzip2 --version` alone, the error message about compressed data sounds like it is attempting to compress something. Do you get the same behavior if you run the command in a different directory? – terdon Jan 31 '14 at 20:09
  • @terdon I tried running it in different directories and I always get the same. I'm running Arch linux and `zsh`, but the same happens in `bash`. – Msegade Jan 31 '14 at 20:25
  • Very strange, the message about compressed data means it is actually trying to compress something for some reason. Both you and @illuminÉ seem to get the same thing but I can't reproduce it on Debian. I wonder if `/usr/bin/bzip2` is not a link to a script or similar. – terdon Jan 31 '14 at 20:28
  • @terdon I have bzip2 in /bin but behavior is similar and output identical to OP. I do need to interrupt too to get the prompt back. I'm on Gentoo, 64bit only. –  Jan 31 '14 at 20:51

1 Answers1

7

The difference is that gzip --version outputs to STDOUT while bzip2 --version outputs to STDERR.

Merge STDERR into STDOUT and you'd see the expected:

$ bzip2 --version 2>&1 | head -n1
bzip2, a block-sorting file compressor.  Version 1.0.6, 6-Sept-2010.
devnull
  • 10,541
  • 2
  • 40
  • 50