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
Call to action
Did you like this post? You can help me back in several ways: (use the "reply" link on the right to comment or to contact me )
- Report any error/typo.
- Report something that could be improved.
- Like and repost!
- Follow me on Bluesky 🦋
- Subscribe to the RSS feed.
- Click on the More on Stackoverflow buttons to make me win "Announcer" badges 🏅.
Thank you for reading! And see you soon on Strangebuzz! 😉