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.

in man bash, I saw the environment variable COLUMNS is the terminal width. I played with its different values, but it doesn't seem to be the actual terminal width in terms of number of characters:

$ dpkg -l | grep mozilla
ii  rhythmbox-mozilla                      2.96-0ubuntu4.3                         Rhythmbox Mozilla plugin
ii  totem-mozilla                          3.0.1-0ubuntu21.1                       Totem Mozilla plugin
$ echo $COLUMNS
135
$ COLUMNS=13 dpkg -l | grep mozilla
ii  totem-mozilla  3.0.1-0ubuntu2 Totem Mozilla plugin
$ COLUMNS=1 dpkg -l | grep mozilla
ii  totem-mozilla  3.0.1-0ubuntu2 Totem Mozilla plugin
$ COLUMNS=100 dpkg -l | grep mozilla
ii  rhythmbox-mozilla   2.96-0ubuntu4.3     Rhythmbox Mozilla plugin
ii  totem-mozilla       3.0.1-0ubuntu21.1   Totem Mozilla plugin
$ COLUMNS=200 dpkg -l | grep mozilla
ii  rhythmbox-mozilla                            2.96-0ubuntu4.3                              Rhythmbox Mozilla plugin
ii  totem-mozilla                                3.0.1-0ubuntu21.1                            Totem Mozilla plugin

What is the terminal width by COLUMNS? Thanks.

share|improve this question

2 Answers 2

COLUMNS is a variable set by bash, and is not meant to be a variable you set manually. It's also not exported by default, so applications launched by the shell don't even see it.

What is it for then? It contains your terminal emulator's width in characters. It's vertical equivalent is LINES. They are both used by the select shell built-in. The select built-in is used for presenting the user with a list of items they can select. It then tries to display the list such that it fits within the terminal. Examples below:

With a 35x50 terminal window:

$ select foo in a b c d e f g h i j k l m n o p q r s t u v w x y z; do echo foo="$foo"; done
1) a    7) g  13) m  19) s  25) y
2) b    8) h  14) n  20) t  26) z
3) c    9) i  15) o  21) u
4) d   10) j  16) p  22) v
5) e   11) k  17) q  23) w
6) f   12) l  18) r  24) x
#? 

With a 100x10 terminal window:

$ select foo in a b c d e f g h i j k l m n o p q r s t u v w x y z; do echo foo="$foo"; done
1) a    3) c   5) e   7) g   9) i  11) k  13) m  15) o  17) q  19) s  21) u  23) w  25) y
2) b    4) d   6) f   8) h  10) j  12) l  14) n  16) p  18) r  20) t  22) v  24) x  26) z
#? 
share|improve this answer
    
man uses $PAGER (or less if unset). $COLUMNS isn't exported by the shell, meaning programs can't use it, because they don't have access to it. –  Patrick Jul 11 at 1:14
    
Thank you for checking about man. I was talking about MANWIDTH. But as you explain it's about the underlying terminal. –  Amphiteóth Jul 11 at 2:59

$COLUMNS gives the terminal width. Concerning the use with dpkg -l, you should look at the longest line (i.e. do not use grep as it will probably discard this line). And of course, if $COLUMNS is too small, such as 1 or 13, one can do nothing for you.

EDIT: if the dpkg -l output is not connected to a terminal, e.g. when piping to grep, dpkg uses column sizes based on the longest fields; thus the output may be wider than the terminal. If you want to take the terminal width into account, use:

COLUMNS=$COLUMNS dpkg -l | grep ...

Note that the shell variable $COLUMNS is not exported by default (i.e. not an environment variable), and COLUMNS=$COLUMNS has the effect to provide COLUMNS as an environment variable for dpkg.

share|improve this answer
    
thanks. If not using grep, how can I find out packages related to "mozilla", without discarding the longest line? –  Tim Jul 11 at 0:27
    
What I meant is that dpkg -l chooses the layout based on the longest line. But if you grep the output, the only sensible contexts are $COLUMNS undefined in the environment (the default) and $COLUMNS set to the terminal width (I've just edited my answer on this point). –  vinc17 Jul 11 at 0:56

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.