How can I find out the number of maximum rows and character columns in an open bash terminal window?

I know I can find out using the curses C library like this:

getmaxyx(stdscr, mrow, mcol);

I would like to associate the mrow and mrol bash variables to my bash session in the same way.

link|improve this question

73% accept rate
feedback

2 Answers

up vote 2 down vote accepted

Bash already has the variables you are looking for built in: $LINES and $COLUMNS.

Unfortunately these only work in interactive shells and not generally for scripts, but it is possible to set the script to be interactive by adding a switch to the script's shebang line:

#!/bin/bash -i

link|improve this answer
feedback

Xterm comes with a tool called resize, which can be used inside scripts to set the same variables:

eval $(resize)
echo "The screen is $COLUMNS columns wide."
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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