2 Methods to Get Terminal Size on Linux

2 Methods to Get Terminal Size on Linux

The terminal window has a defined number of rows and columns, and knowing these dimensions can be useful when working with shell scripts, command-line tools, and terminal-based interfaces. This tutorial provides 2 methods how to get terminal size on Linux.

Method 1 - tput command

The tput command can be used to query terminal capabilities and retrieve its current dimensions. The lines and cols options return the number of rows and columns, respectively.

To get the number of terminal rows, run:

tput lines

To get the number of terminal columns, run:

tput cols

Method 2 - stty command

The stty command can display various terminal settings, including the current number of rows and columns. Its size option outputs both values, while awk can extract either value separately.

To get the number of terminal rows, run:

stty size | awk '{print $1}'

To get the number of terminal columns, run:

stty size | awk '{print $2}'

Leave a Comment

Cancel reply

Your email address will not be published.