Console geometry refers to the dimensions of the terminal window—specifically, the number of columns and rows. These values determine how much text can fit on a single line and how many lines can be displayed at once.

Knowing the size of the terminal allows your script to adapt its output, avoiding awkward line breaks or misaligned text. In this article, we’ll walk through how to retrieve console geometry, explain why it’s useful, and show you practical examples.

Retrieving Console Geometry in Bash

There are a few ways to retrieve the console geometry in a Bash script. We’ll cover three methods: using tput, stty, and command substitution.

Method 1: Using tput

The tput command is a simple way to get terminal dimensions. Here’s how you can use it:

#!/bin/bash

# Get the number of columns
cols=$(tput cols)
# Get the number of rows
rows=$(tput lines)

echo "Terminal size: ${cols} columns, ${rows} rows"

In this script, tput cols returns the number of columns, and tput lines returns the number of rows. We store these values in variables and then print them out.

The above script will produce the following output.

Method 2: Using stty

Another way to retrieve terminal size is with the stty command:

#!/bin/bash

# Get terminal size
size=$(stty size)
rows=$(echo $size | cut -d' ' -f1)
cols=$(echo $size | cut -d' ' -f2)

echo "Terminal size: ${cols} columns, ${rows} rows"

The stty size command gives you the number of rows and columns in a single line. We split this line into two variables using the cut command.

After running the above script, the output looks like shown below:

Terminal size: 168 columns, 43 rows

Method 3: Using Command Substitution

Command substitution lets you combine commands to get the geometry in one go:

#!/bin/bash

# Get terminal size
cols=$(echo $(stty size) | cut -d' ' -f2)
rows=$(echo $(stty size) | cut -d' ' -f1)

echo "Terminal size: ${cols} columns, ${rows} rows"

This method is similar to the stty example, but it uses command substitution to streamline the process.

You will get the following result after running the script.

Terminal size: 168 columns, 43 rows

Using Geometry Values in a Script

Now that you know how to get the terminal size, let’s use these values in a script. For instance, you might want to center some text based on the terminal width:

#!/bin/bash

# Get terminal size
cols=$(tput cols)

# Text to display
text="Welcome to Bash Scripting!"

# Calculate the position to center the text
padding=$((($cols - ${#text}) / 2))

# Print the centered text
printf "%${padding}s%s\n" "" "$text"

Here, ${#text} gets the length of the text string, and padding calculates how much space to add on the left to center the text.

This script will generate the following output.

                                  Welcome to Bash Scripting!

Conclusion

Retrieving console geometry in a Bash script is a valuable skill. It allows you to make your scripts more interactive and responsive to the user’s environment. Whether you use tput, stty, or command substitution, these methods allow you to format your output dynamically.

Now that you know how to retrieve and use console geometry. You can start making your scripts more user-friendly and adaptable on dedicated server hosting from Atlantic.Net!