1. Overview

Knowing terminal dimensions is useful in a variety of situations, especially when creating dynamic and responsive command-line interfaces. This information allows us to adjust our script output according to the terminal size, resulting in a better user experience. Furthermore, understanding the terminal size enables us to properly format text, avoid line wrapping, and produce more visually appealing results.

In this tutorial, we’ll look at how to get the console geometry in a Bash script.

2. Using the tput Command

The tput command, commonly used on Linux systems, is part of the ncurses library. Furthermore, using tput is simple and requires minimal setup, as ncurses is usually installed by default on most Linux distributions. It’s also relatively fast, making it useful for scripts that need to access terminal dimensions repeatedly. Additionally, it provides a simple and efficient method for querying terminal capabilities, including size:

cols=$(tput cols)
rows=$(tput lines)

echo "Terminal width: $cols"
echo "Terminal height: $rows"

In this example, the tput cols command returns the number of columns and width in the terminal. The tput lines command returns the number of rows and height in the terminal. We save these values in the variables cols and rows, and then print them.

Finally, if we’re building a script that requires a dynamically scaled progress bar or table, knowing the terminal width allows us to modify the output to avoid wrapping or truncation, resulting in a cleaner and more professional look.

3. Using the stty Command

The stty command is a versatile tool that allows us to alter and query terminal line settings. It’s a standard tool available on most Linux systems, therefore, it’s a reliable option for obtaining terminal dimensions.

Furthermore, stty is an effective tool because it’s widely available and requires no additional libraries. However, parsing the output necessitates an additional step using the cut command, which may add some complexity to our script. However, its availability makes it a dependable option for most situations. stty can also be integrated into our script:

geometry=$(stty size)

rows=$(echo $geometry | cut -d' ' -f1)
cols=$(echo $geometry | cut -d' ' -f2)

echo "Terminal width: $cols"
echo "Terminal height: $rows"

In this example, stty size is the command that returns the terminal size in rows and columns format. Then, we store the output in the geometry variable. Thereafter, we use the cut command to split the output and extract the number of rows and columns. Finally, we print the values.

4. Using ANSI Escape Codes

ANSI escape codes are a more adaptable solution that can be used in a variety of terminal emulators. They’re particularly helpful in situations where we need to dynamically react to terminal resizing events.

Additionally, using ANSI escape codes allows us to gain greater flexibility and control over terminal interactions. This method is beneficial when working with terminal emulators, as it allows us to manage dynamic scaling efficiently:

echo -ne "\033[999;999H\033[6n"

IFS=';' read -sdR -p $'\E[6n' ROW COL

rows=${ROW#*[}
cols=$COL

echo "Terminal width: $cols"
echo "Terminal height: $rows"

In this example, we use echo -ne “\033[999;999H\033[6n” to move the cursor to the bottom-right corner before querying its location. Then, IFS=’;’ read -sdR -p $’\E[6n’ ROW COL reads the answer from the terminal and includes the current cursor position in the format ESC[row;colR. Furthermore, we then extract the row and column numbers from the response and save them in rows and cols. Finally, we print the results at the end of the script.

This method is suitable for interactive scripts in which the terminal size can change during execution. For example, in a real-time monitoring application, we may wish to dynamically alter the layout as the user resizes the terminal window.

5. Using Environment Variables

Bash also has the $LINES and $COLUMNS environment variables, which store the terminal’s current number of rows and columns. The terminal emulator automatically modifies these variables, making it an excellent solution for users.

This approach is also great for lightweight scripts that need to adjust their output to fit the terminal size without using advanced commands. For example, a script that displays a dynamically resized banner or header can use $COLUMNS and $LINES to change the layout:

$ echo "Terminal width: $COLUMNS"
$ echo "Terminal height: $LINES"

In this example, we use the $COLUMNS and $LINES environment variables to determine the terminal width and height, respectively. This method is basic and requires no additional parsing.

Finally, this method is one of the simplest ways to retrieve terminal dimensions, and it’s available on a large number of Bash configurations. It’s also useful for short scripts or when we need to alter the terminal size multiple times within a script.

6. Conclusion

Understanding how to get the console geometry in a Bash script allows us to tailor our Bash scripts to the terminal environment, thus improving the user experience.

In this article, we’ve looked at various techniques for obtaining console geometry in Bash. Methods include using the tput command, stty, the ANSI escape codes, and Bash environment variables. Furthermore, we can choose these approaches based on the specific requirements of our script, as they provide a variety of benefits and offer various advantages from one approach to the next.