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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
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'
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0