How do I find number of vertical lines available in the terminal?

I’m writing a script which shows the git log for a directory when I cd into it.

Such a log can be overwhelming, containing hundreds of lines. So far I have been limiting that to a hard-coded 30 lines with (... | head -n 30), cool on the big screen at Work, too big at home. I would prefer the log to take up about half the (vertical) screen on any terminal, e.g. Gnome at work, iTerm2 at home. I do not use screen or tmux. But would like to learn them.

How do I find the number of vertical lines available in a terminal from command line?

Asked By: jalanb


This command should give you the number of lines on the terminal:

stty size | cut '-d ' -f1

Some systems might not implement stty size so you might need something like this instead:

stty -a | tr ; \012 | grep rows | tr -d ' rows'
Answered By: Celada

Terminal parameters are stored as $LINES and $COLUMNS variables.

Additionally you can use special term-operation programm, for example tput:

tput lines  # outputs the number of lines of the present terminal window.
tput cols   # outputs the number of columns of the present terminal window.
Answered By: Costas