Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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.

share|improve this question
    
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." –  Riguefort Ultraquaillette 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

2 Answers 2

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.
share|improve this answer
    
That's better, but still doesn't work as expected, I have to press Ctrl-C as before. –  Msegade Jan 31 '14 at 13:50
    
@Msegade That's odd. I just tried it and don't need to interrupt to get the prompt. –  devnull Jan 31 '14 at 13:51

Yes, --version/--license outputs to stderr and, as pointed out by devnull, you'll have to redirect it in order to filter it.
As to the program not quitting and requiring Ctrl-C to terminate... this is the default behaviour. Since you didn't specify any file names:

If no file names are specified, bzip2 compresses from standard input to standard
output. In this case, bzip2 will decline to write compressed output to a terminal
as this would be entirely incomprehensible and therefore pointless.

So, when you run just bzip2 --version the output is a terminal and the program exits with the following message:

bzip2: I won't write compressed data to a terminal.

but when you pipe it to head the output is no longer a terminal so the program is waiting for input from stdin. If you add a -v (--verbose) you will actually get the confirmation:

bzip2 --version -v | head -n 1
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.

  (stdin):

and it just sits there waiting for your input until you terminate it with Ctrl-C. You could actually start typing a few lines (instead of terminating the program) then hit Ctrl-D twice and see the result...
debian users don't get the same behaviour because their version was patched to exit when using --version/--license:

@@ -1916,8 +1918,8 @@ IntNative main ( IntNative argc, Char *a
       if (ISFLAG("--keep"))              keepInputFiles   = True;    else
       if (ISFLAG("--small"))             smallMode        = True;    else
       if (ISFLAG("--quiet"))             noisy            = False;   else
-      if (ISFLAG("--version"))           license();                  else
-      if (ISFLAG("--license"))           license();                  else
+      if (ISFLAG("--version"))           { license(); exit ( 0 ); }  else
+      if (ISFLAG("--license"))           { license(); exit ( 0 ); }  else
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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