Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

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

share|improve this answer
add comment

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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