getting column width in various shells

Chris F.A. Johnson cfaj-uVmiyxGBW52XDw4h08c5KA at public.gmane.org
Wed Nov 8 15:57:10 UTC 2006


On Wed, 8 Nov 2006, Madison Kelly wrote:

> Hi all,
>
>  I am trying to add line-wrapping to my program and to this end I am trying 
> to figure out the different ways of getting the current number of columns 
> from the different shells. I am hoping you guys who use shells other than 
> bash or sh might be able to help me build a list.
>
> For 'bash' and 'sh',

    On a Linux system, sh IS bash. There are some small (in size)
    distros where another shell is used.

    Are you writing only for Linux?

> I know this works:
> $ echo $COLUMNS

    It may work, but it may be wrong. If the window is resized, the
    variable will not be updated if the shell is not in control (i.e.,
    if another program is running).

> For 'csh' and 'tsch',

    On a Linux system, csh and tcsh are the same shell.

> I *thought* it was the same, and the man page seems to 
> agree, but I only get an error:
>
>>  echo $COLUMNS
> COLUMNS: Undefined variable.

    If they are set, the shell will update them when the window is
    resized.

> Beyond these four, I am not even really sure what shells are in common use, 
> so feedback on that would be helpful, too.

    ksh, [d]ash, zsh.

    However, that is the wrong way to go about it. You need to get the
    result independently of the shell.

    There are three commands that may give you the information. They
    are not standardized:

tput cols
stty size
resize

    You might be better off looking at the source code for one or more
    of those commands and using the underlying system functions.

-- 
    Chris F.A. Johnson                      <http://cfaj.freeshell.org>
    ===================================================================
    Author:
    Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list