25

Within a desktop environment we can resize terminals (GNOME Terminal for example) for our convenience.

How can I know the size of the terminal in terms of pixels or number of columns and rows?

Alex Jones
  • 6,353

3 Answers3

36

If you issue the command

stty size

it returns the size of the current terminal in rows and columns. Example:

$ stty size
24 80

You can read the rows and columns into variables like this (thanks to Janis' comment):

$ read myrows mycols < <(stty size)

Obtaining the size in pixels requires knowledge of your screen's resolution and I don't think stty has direct access to such information.

Joseph R.
  • 39,549
  • 1
    sir is it not possible to get output in terms of pixels ? – Alex Jones Apr 19 '15 at 01:54
  • 1
    Note that in bash the echo command in this answer will not see the variables, since the read in the pipeline will be executed in a subshell. (It works in ksh, though.) For bash you may want to use, e.g., read myrows mycols < <( stty size ). – Janis Apr 19 '15 at 01:59
  • Every Linux distro I've used has resize which outputs a shell script to set the size in environment variables. – symcbean Sep 15 '21 at 13:50
  • 1
    No, you can't get the pixel dimensions of a tty because its a character device and doesn't have pixels. – symcbean Sep 15 '21 at 13:50
6
tput cols; tput lines

This will display columns and rows.

AdminBee
  • 22,803
Drek
  • 61
3

In a desktop environment, you are using X, and the xwininfo utility can show the size of the window in pixels. Also, if you are running on the desktop (and not, for instance, remotely connected), the terminal emulator provides a variable $WINDOWID which you can use as a parameter to xwininfo, e.g.,

xwininfo -id $WINDOWID

and get a listing list this:

xwininfo: Window id: 0xc00025 "uxterm"

  Absolute upper-left X:  65
  Absolute upper-left Y:  167
  Relative upper-left X:  0
  Relative upper-left Y:  22
  Width: 624
  Height: 577
  Depth: 24
  Visual: 0x22
  Visual Class: TrueColor
  Border width: 1
  Class: InputOutput
  Colormap: 0x21 (installed)
  Bit Gravity State: NorthWestGravity
  Window Gravity State: NorthWestGravity
  Backing Store State: NotUseful
  Save Under State: no
  Map State: IsViewable
  Override Redirect State: no
  Corners:  +65+167  -589+167  -589-256  +65-256
  -geometry 103x42+65+145

In this example, the lines with Width and Height are the size in pixels. The last line, with -geometry gives the size in characters (as well as the position of the upper-left corner — in pixels).

Talking of resizing the window, the resize program shows the number of lines and columns. For this example, it shows

$ resize
set noglob;
setenv COLUMNS '103';
setenv LINES '42';
unset noglob;

The question did not indicate how the information might be used, but since the output is text, in a predictable format, it is readily scripted. Here is a simple example using awk:

#!/bin/sh
if [ -n "$WINDOWID" ]
then
    xwininfo -id $WINDOWID | awk '
    BEGIN { px = 0; py = 0; chars = "?x?"; }
    /Height:/ { py = $2; }
    /Width:/ { px = $2; }
    /-geometry/ { chars = $2; sub("+.*","",chars); }
    END { printf "%dx%d pixels, %s chars\n", py, px, chars; }'
else
    printf '? no WINDOWID found\n'
fi

which prints

577x624 pixels, 103x42 chars
Thomas Dickey
  • 76,765