Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

terminal

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 
like image 361
too much php Avatar asked Nov 04 '08 23:11

too much php


People also ask

What is the formula to find width?

If you have the area A and length h , its width w is w = A/h . If you have the perimeter P and length h , its width is w = P/2−h . If you have the diagonal d and length h , it's width can be found with w = √(d²−h²) .

How do you find the width with the area?

To calculate the length and width of a rectangle first, calculate the value of width 'w' by using the area of rectangle formula that is, 'w = A/l'. Then substitute the value of width in the formula of the perimeter of a rectangle and simplify the value of length 'l', that is, P = 2 (l + A/I).

What is a width example?

The distance from side to side. Example: the width of this door is 80 cm.

What is the width of a rectangle?

To find the width of a rectangle, use the formula: area = length × width. Just plug the area and length of the rectangle into the formula and solve for the width. If you don't have the area, you can use the rectangle's perimeter instead. In that case, you would use the formula: perimeter = 2 × length plus 2 × width.


2 Answers

  • tput cols tells you the number of columns.
  • tput lines tells you the number of rows.
like image 188
TonyUser Avatar answered Sep 23 '22 07:09

TonyUser


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)

like image 27
David Dean Avatar answered Sep 23 '22 07:09

David Dean