Take the 2-minute tour ×

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.

share|improve this question

2 Answers 2

up vote 3 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

share|improve this answer

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."
share|improve this answer

Your Answer


