How to find width and height of the terminal in linux

You may wonder how do I find the number of characters will fit in a line in the terminal or size of the window. There are several ways to figure out the size of the window. Let’s go through the different ways to find size of terminal

First lets see different ways that you can use to figure out width and height of terminal in terms of characters

  1. Using command tput
  2. Using command stty
  3. Using environment variables

    Using command tput

The command tput is very helpful to query the terminal information and to do some other simple operations like placing cursor at required random position.

Here is how you can find the width and height of terminal. You may call it as number of rows and columns

$ tput lines
$ tput cols
$ echo "columns: `tput cols` Rows: `tput lines` "
columns: 141 Rows: 36 

Using command stty

This is also one of the command which useful to query,change and print terminal line settings.

The simple output of stty command regarding information of terminal

speed 38400 baud; line = 0;
eol = M-^?; eol2 = M-^?; swtch = M-^?;
ixany iutf8

Command to get lines and columns count in characters

$ stty size
39 143


Using environment variables

On you terminal always you can query the size of the terminal using the environment variables $COLUMNS

$ echo "$LINES $COLUMNS"
39 143
$ echo "$LINES $COLUMNS"   # Resized the window
22 79

Values of those environment variables will change if size of the terminal window changes.

Leave a Reply

Your email address will not be published. Required fields are marked *