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

6,993

Solution 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.

Solution 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'
Share:
6,993

Related videos on Youtube

jalanb
Author by

jalanb

Still alive, still coding

Updated on September 18, 2022

Comments

  • jalanb
    jalanb almost 2 years

    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?

  • Warren Young
    Warren Young over 9 years
    This seems very OS-specific. Can you compile the list of systems where each incantation works?
  • Celada
    Celada over 9 years
    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.
  • mr.spuratic
    mr.spuratic over 9 years
    LINES and COLUMNS are only set by some shells. bash sets them, but only for an interactive shell (and it does not export them).
  • jamesqf
    jamesqf over 9 years
    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 :-)
  • Costas
    Costas over 9 years
    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'
  • Celada
    Celada over 9 years
    @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
    Celada over 9 years
    @jamesqf bash documentation says "Automatically set upon receipt of a SIGWINCH." so, yes, it does change them if you resize the terminal window.
  • Costas
    Costas over 9 years
    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.
  • coder
    coder over 2 years
    Second solution doesn't work on my system. First line gives me a number but I can zoom into the terminal (ctrl - shfit - +) and the number doesn't change. But number of lines visible on the terminal definitely does. I'm using linux mint, xfce, bash.
  • coder
    coder over 2 years
    That command returns a number but I can zoom into the terminal (ctrl - shfit - +) and the number doesn't change. But number of lines visible on the terminal definitely does. I'm using linux mint, xfce, bash.