How big is my terminal?
While 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 ))
May 7th, 2008 at 10:00 pm
1b09672fa466…
1b09672fa466421615e8…