![在 Bash 中反转整行的颜色](https://linux22.com/image/174253/%E5%9C%A8%20Bash%20%E4%B8%AD%E5%8F%8D%E8%BD%AC%E6%95%B4%E8%A1%8C%E7%9A%84%E9%A2%9C%E8%89%B2.png)
答案1
这很简单,你可以试试这个:
# Reset
reset='\033[0m'
# White Background
BG='\033[47m'
# Black Foreground
FG='\033[0;30m'
# Usage
echo -e "$FG$BG This will print black text on white background $reset"
如果你想要整行:
reset='\033[0m'
BG='\033[47m'
FG='\033[0;30m'
text="A black text on white"
cols=$(tput cols)
# Left Aligned
x=$((cols-${#text}))
printf "$FG$BG%s%*s$reset\n" "$text" $x
# Centered text
x_center=$(((${#text}+cols)/2))
x_rest=$((cols-x_center))
printf "$FG$BG%*s%*s$reset\n" $x_center "$text" $x_rest