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 20 lines (... | head -n 20), which is fine on the screen at work, but too much on the smaller MacBook screen at home.

I would prefer the log to take up about half the (vertical) screen on either terminal. And "terminal" also changes: it's Gnome terminal at work, but iTerm2 at home. And I do not use screen or tmux.

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


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.
| improve this answer | |
  • 9
    LINES and COLUMNS are only set by some shells. bash sets them, but only for an interactive shell (and it does not export them). – mr.spuratic Feb 10 '15 at 18:00
  • 1
    The environment vars don't exist in my tcsh. Also, I wonder if the system would reset them if you resize the terminal window. Both tput and stty do work for me, so I learned something new :-) – jamesqf Feb 10 '15 at 18:40
  • @jamesqf bash documentation says "Automatically set upon receipt of a SIGWINCH." so, yes, it does change them if you resize the terminal window. – Celada Feb 11 '15 at 3:30
  • For autoset above vaiable the following options is responsible shopt -s checkwinsize which added in my /etc/bash.bashrc file with comment: # check the window size after each command and, if necessary, update the values of LINES and COLUMNS. – Costas Feb 11 '15 at 12:20

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'
| improve this answer | |
  • 1
    This seems very OS-specific. Can you compile the list of systems where each incantation works? – Warren Young Feb 10 '15 at 13:52
  • 4
    I was afraid that the first one might be OS specific, which is why I added the second one. Indeed after a little bit of Googling I see that the first one probably won't work on Solaris. The second one should be very portable. Honestly, @Costas' tput lines seems like the better answer and I have upvoted it. I didn't know about it before. – Celada Feb 10 '15 at 13:57
  • 1
    I'd like to offer substitute long pipe with stty -a by something like grep -Po 'rows \K[^;]*' or sed -n 's/.*rows \([^;]*\).*/\1/p' – Costas Feb 10 '15 at 18:48
  • @Costas actually, frustratingly, neither my command nor either one of yours worked on MacOS, so I had to come up with yet a different command (edited). Go figure: MacOS emits "24 rows" instead of "rows 24"! I still think tput lines is way better, and I think it's pretty portable! – Celada Feb 11 '15 at 3:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.