Getting the columns count of the terminal

Published on 2021-12-21 • Modified on 2021-12-21

In this snippet, we see how to get the columns count of the terminal. The two following commands produce the same result. In the first case, we output the $COLUMNS shell variable, and in the second, we use the tput command. This information can be important because when outputting something to the terminal (for example, in a Symfony command), carriage return "\n" may be inserted depending on this size, and it can potentially break the assertions you make about what is displayed. Note that $COLUMNS is a shell variable, not an environment variable. It's also dynamic, it changes as soon as the terminal is resized!


echo $COLUMNS # 135
tput cols     # 135

 More on Stackoverflow   Read the doc  More on the web  Random snippet

  Work with me!