2017-05-13 74 views
0

我一直在製作一個Bash腳本,上面寫着「由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" 

爲什麼是文字坐向左而不是居中?

+0

它僅僅是第二個不居中的字符串嗎? – 123

回答

1

您的「真實」字符串具有由顏色定義引起的額外長度,但這些定義未顯示在終端中。每個定義的長度是12,你有4個定義,所以12x4 = 48,48/2 = 24。將最後一行更改爲

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