Skip to content

Instantly share code, notes, and snippets.

@TrinityCoder
TrinityCoder / center_text_in_bash.md
Last active April 18, 2024 12:02
How to center text in Bash

Sometimes we might want to print some text in Bash and we might want it to be centered to the centre of the terminal. It is a cheap way how we can increase clarity of output of our script and make it look much more attractive.

The whole magic is hidden in a program called tput.

To get number of rows and cols of current terminal, we need just two simple shell substitutions:

    TERM_ROWS="$(tput rows)"