Alex Rivera | Logout

How do I find the width & height of a terminal window?

Asked 2008-11-04T23:33:47.217
441

As a simple example, I want to write a CLI script which can print = across the entire width of the terminal window.

#!/usr/bin/env php
<?php
echo str_repeat('=', ???);

or

#!/usr/bin/env python
print '=' * ???

or

#!/usr/bin/env bash
x=0
while [ $x -lt ??? ]; do echo -n '='; let x=$x+1 done; echo
Edit
Report

2 Answers

149

In bash, the $LINES and $COLUMNS environmental variables should be able to do the trick. The will be set automatically upon any change in the terminal size. (i.e. the SIGWINCH signal)

answered 2009-02-19T01:30:49.907
24
yes = | head -n$(($(tput lines) * $COLUMNS)) | tr -d '\n'
answered 2008-11-04T23:50:03.893

Your Answer