为什么这个文字没有居中?

时间:2017-05-13 18:17:46

标签: bash alignment

我一直在制作一个Bash脚本,上面写着“Made by:Keegan Kuhn”。但是,我无法将文本置于中心位置。

一开始,我定义了一些颜色来制作多色文字:

# Defines colors
black='tput setaf 0'
red='tput setaf 1'
green='tput setaf 2'
yellow='tput setaf 3'
purple='tput setaf 4'
pink='tput setaf 5'
skyBlue='tput setaf 6'
white='tput setaf 7'
grey='tput setaf 8'

我遇到问题的部分是最后几行:

string="Disguise your MAC Address as that of any manufacturer as you want."
sleep .2; printf "%*s\n" $(( ( ${#string} + $(tput cols) ) / 2 )) "$string" # Centers variable "string"
echo
string="$($skyBlue)Made by:$($green) Keegan Kuhn ($($red)keeganjk$($green))"
sleep .2; printf "%*s\n" $(( ( ${#string} + $(tput cols) ) / 2 )) "$string" # Centers variable "string"

为什么文字位于左侧而不是居中?

1 个答案:

答案 0 :(得分:1)

您的“真实”字符串具有由颜色定义引起的额外长度,但这些定义未在终端中显示。每个定义的长度为12,您有4个定义,因此12x4 = 48,48 / 2 = 24。将最后一行更改为

sleep .2; printf "%*s\n" $(( ( ${#string} + 24 + $(tput cols) ) / 2 )) "$string" # Centers variable "string"