Skip to content

Center text on the terminal

Print a line padded so the text sits in the middle of the terminal width.

18th August 2026

center_text() {
  textsize=${#1}
  span=$(((screen_width + textsize) / 2))

  printf '%*s\n' "${span}" "$1"
}

get_screen_width() {
  screen_width=$(tput cols)
  declare -g screen_width
}

Caveats

Call get_screen_width first, or set screen_width yourself. Requires tput.