get the width of the terminal

A quick and easy way to get the width of your terminal is the command stty size. I have used it with diff like this:

diff -y -W `stty size | cut -d” ” -f2` –suppress-common-lines oldfile newfile

Note: This stty option is not available on Solaris, however, if you have it installed, the /usr/openwin/bin/resize command sets the COLUMNS variable.

update: This post originally said “width of your Linux terminal” but as noted in the comments, this feature of stty is also available in *BSD implementations, even though it is not part of the POSIX standard. So you should expect this to work on GNU and BSD systems (eg, most GNU/Linux distros, most *BSDs, including OSX) but not on all POSIX-compliant systems (eg, Solaris). I would assume that AIX, HPUX, SCO, the other “traditional” UNIX systems would also not support this, though I have not (yet) tested any of them. YMMV.

5 Responses to get the width of the terminal

  1. kamper says:

    Saying “Linux terminal” isn’t exactly fair. “stty size” works for me on os x and OpenBSD and in particular, the STANDARDS section of the man page of the latter says:

    “The stty utility is compliant with the IEEE Std 1003.1-2004 (“POSIX”) specification.

    “The flags [-ef] and the operands speed, crtscts, iuclc, imaxlabel, onlcr, olcuc, oxtabs, onoeot, echoke, altwerase, mdmbuf, flusho, pendin, xcase, tty, crt, kerninfo, columns, cols, rows, dec, extproc, raw, size, the compatibility modes and the control operations are extensions to that specification.”

    Your use of “diff”, though, is not very portable 😉

  2. unixshell says:

    True. Fair comment, and as this blog is explicitly called “*nixshell” for that reason, I hold my hands up entirely.

    As you cite: “The stty utility is compliant with … POSIX … The … operands … speed, raw, … size … are extensions to that specification”

    That was really what I was trying to communicate – not that it is a Linux feature, but that it’s a non-POSIX feature. I only had Linux and Solaris machines to hand when I posted; thanks for confirming that it works on the *BSD systems too.

    Regarding “diff” – I was using GNU diff; sdiff can be useful too, but this post was inspired by a Linux-centric installation I have been working on where I can be sure of bash and GNU diff.

    As a related item – I should probably post an entry on this subject – when writing scripts which assume (say) bash, the first line must say “#!/bin/bash”, not “#!/bin/sh”. It may even be necessary to get more specific, even to the level of “#!/usr/local/perl/perl5.8/bin/perl” if you require a certain feature, so that you can be sure that you have documented your requirements

  3. Jon Disnard says:

    Your example of stty is unspecified in Posix.
    A better solution would be to use tput(1).
    It will work on Unix and Linux systems.

    So far I tested it as working on Solaris, HP-UX, RHEL, and FreeBSD.

    Width == `tput cols`
    Height == `tput lines`

    To get both at once:
    printf “cols\nlines\n” | tput -S
    [. . .]

    In regards to the resize(1) cmd, I would imagine that ccould be (re)implemented on any systems without it by using the above example in a shell function:

    resize () {
    COLUMNS=`tput cols`
    LINES=`tput lines`
    export COLUMNS LINES
    }

    If you’re on a Solaris system and choose to use the resize(1) that is found there, make sure to invoke it this way:
    eval `resize`

    That way the environment is updated.
    a nice trick would be to put that in the users $PS1 prompt to evaluate anytime the shell prompts, presumably after a resize event.

  4. unixshell says:

    Thanks Jon for such a complete and clear post.

Leave a comment