Since this question is popular, I want to add a newer answer with more information.
The $COLUMNS and $LINES variables are not environment variables. These values ββare dynamically set by the shell after each command, and we usually cannot access it from non-interactive scripts.
Bash sets these variables when we checkwinsize parameter using:
shopt -s checkwinsize
On many systems, this option is used for the default file or for the entire system file ( bashrc ), so we need to remember that these variables may not be available. On some systems, such as Cygwin, this option is not enabled for us, so Bash will not install $COLUMNS and $LINES unless we run the line above or add it to our .bashrc .
When writing non-interactive scripts, we cannot use $LINES and $COLUMNS by default. Instead, the stty and tput utilities provide portable tools for determining the size of a terminal from a script (the commands that are currently being standardized for POSIX are described below).
As shown in the current accepted answer, we can use tput to compile the size of the terminal quite simply:
lines=$(tput lines) columns=$(tput columns)
Alternatively, the size request for stty gives us the number of rows and columns of the terminal in one step (displayed as the number of columns followed by two spaces followed by the number of rows):
size=$(stty size)
I sometimes prefer the stty approach because we invoke another command and subshell (expensive on Cygwin), but it requires us to analyze the output in rows and columns, which may be less readable:
lines=${size#* } columns=${size% *}
Both approaches described above work in any POSIX shell. For Bash in particular, we can use process overrides to simplify the previous example:
read columns lines < <(stty size)
... which works faster than the tput example, but still slower than the first stty implementation, at least on my machine. In practice, the performance impact is probably negligible - choose the approach that is best for the program (or based on which command is available on the target system).
If for some reason we want to use $LINES and $COLUMNS in our scripts, we can configure Bash to export these variables to the environment:
trap 'export LINES COLUMNS' DEBUG
The DEBUG trap is executed before each command entered at the prompt, so we can use it to export these variables. By re-exporting them after each command, we guarantee that the environment variables will be updated if the terminal size changes. Add this line to .bashrc . It works great for personal scripts, but I do not recommend using these variables in any script that will be shared.