在 bash 中居中 Justify/align ASCII 艺术

Center Justify/align ASCII art in bash

我刚刚在做我的 bash 项目和

I want the header ascii art to automatically adjust at the center whenever the terminal executes the script in whatever resolution. Is it possible mates? Following is my code:

#!/bin/bash
clear
echo
echo -e "\t\t1▄██████▄#0000▄████████0000▄████████11▄██████▄ ";
echo -e "\t\t███0000███111███0110███111███1011███1███#0000██";
echo -e "\t\t███0001███111███0111███111███1100█▀11███#ffff██";
echo -e "\t\t███0010███11▄███▄▄▄▄██▀11▄███▄▄▄11111███#0000██";
echo -e "\t\t███0011███1▀▀███▀▀▀▀▀111▀▀███▀▀▀11111███#ffff██";
echo -e "\t\t███0100███1▀███████████111███1101█▄11███#0000██";
echo -e "\t\t███0101███111███1000███111███1110███1███#ffff██";
echo -e "\t\t1▀██████▀ffff███1001███111██████████11▀██████▀1";
echo -e "\n\n"

您可以使用 returns 终端宽度的 COLUMNS 环境变量。

banner_width=46
indent=$(( (COLUMNS - banner_width) / 2 ))
prefix=''
for ((i=1; i<=indent; i++)) ; do
    prefix+=' '
done
echo
echo -e "${prefix}1▄██████▄#0000▄████████0000▄████████11▄██████▄ ";