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

前端 未结 9 1032
滥情空心
滥情空心 2020-12-04 04:33

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


        
相关标签:
9条回答
  • 2020-12-04 05:28

    Inspired by @pixelbeat's answer, here's a horizontal bar brought to existence by tput, slight misuse of printf padding/filling and tr

    printf "%0$(tput cols)d" 0|tr '0' '='
    
    0 讨论(0)
  • 2020-12-04 05:33

    And there's stty, from coreutils

    $ stty size
    60 120 # <= sample output
    

    It will print the number of rows and columns, or height and width, respectively.

    Then you can use either cut or awk to extract the part you want.

    That's stty size | cut -d" " -f1 for the height/lines and stty size | cut -d" " -f2 for the width/columns

    0 讨论(0)
  • 2020-12-04 05:34
    • tput cols tells you the number of columns.
    • tput lines tells you the number of rows.
    0 讨论(0)
提交回复
热议问题