I often use dpkg or aptitude combined with grep when I want to list certain packages available or installed on my system, but I noticed that when I add | grep, the output lines look a little bit different.

Here's a pure dpkg output, the first command was typed when the terminal was smaller, the second one when the terminal was maximized:


As you can see, the output differs depending on the size of the window -- the spaces are reduced in the case of a smaller one.

Now, what happens when we add | grep:


A part of the first output was dropped to the second line. But when I maximised the terminal and typed the command once more, the line is in one piece. Moreover, the columns have the same fixed size (the same spaces between them).

This is an aptitude output:


Both commands were typed in the maximised window, but the grep line has narrower columns, and some text of the third column was cut off.

Why does it happen? Is there a way to stop grep from resizing the lines?

I don't know how to add an image without changing its parameters, I hope you see what I'm talking about.


It is not grep changing the output. It is dpkg and aptitude. They check whether the output goes to a terminal or to some other command.

If it is a terminal they adapt their own output width to match the terminal size.

If the output does not go to a terminal, the command has no idea what column size would be appropriate. (The output might as well end in some file.)

The same happens with ls. Compare ls and ls|cat.

There is no general way to solve this, but some commands might have specific options for that. For example aptitude has --disable-columns and -w:

       This option causes aptitude search and aptitude versions to output
       their results without any special formatting. In particular:
       normally aptitude will add whitespace or truncate search results in
       an attempt to fit its results into vertical “columns”.
       With this flag, each line will be formed by replacing any format
       escapes in the format string with the corresponding text; column
       widths will be ignored.

   -w <width>, --width <width>
       Specify the display width which should be used for output from
       the search command (by default, the terminal width is used).

The man page of dpkg says:

          Sets the number of columns dpkg should use when
          displaying formatted text. Currently only used by -l.
  • 1
    Is there a way to set a certain width parameter when using | and other commands? – Mikhail Morfikov Jan 11 '14 at 10:54
  • 3
    The pipe | is something inside your shell, changing the output from the terminal to a new descriptor which is set up as input for the second command. The first command itself can only check, whether its output is a terminal or not. Some commands might support command line options to explicitly set a width, or they might check the $COLUMNS environment variable. But this is totaly up to the command itself. – michas Jan 11 '14 at 11:00
  • Yep, if I set in aptitude the -w option, it works, but it gives me 2 extra empty lines between each normal line. Is there something I can do about it? In case of dpkg it is possible to use the COLUMNS variable -- COLUMNS=100 dpkg -l test | grep test, but this woks only in the case of dpkg, aptitude ignores it. – Mikhail Morfikov Jan 11 '14 at 13:55
  • Yes, this kind of formatting is highly command specific. Each command is able to provide its own way. The extra lines might come from a too large width value. aptitude -k $COLUMNS should be fine. – michas Jan 11 '14 at 14:43
  • 1
    That was a typo of me. I meant aptitude -w $COLUMNS. – michas Jan 11 '14 at 17:03

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.