How big is my terminal?
Wednesday, August 29th, 2007While poking around I discovered that you can use the stty size command to get the dimensions in columns and rows of the current terminal window.
This little bash functions can help. We have to tell stty what terminal (ie tty) to use. By default it’ll use the stdin, which doesn’t work if you’re chaining commands.
TTY=$(tty)
function rows {
stty --file=${TTY} size | sed 's/^([0-9]*) ([0-9]*)/1/'
}
function cols {
stty --file=${TTY} size | sed 's/^([0-9]*) ([0-9]*)/2/'
}
Example usage:
dmesg | tail -n $(( $(rows) - 2 ))