如何使用颜色设置变量并与 bash 左对齐
例子:
red=$(tput setaf 1)
green=$(tput setaf 2)
normal=$(tput sgr0)
if [ $process_state != "running" ]; then
process_state=${red}$process_state${normal}
else
process_state=${green}$process_state${normal}
fi
printf "%-10s|" $process_state
输入
process_state=running
process_state=stopped
输出
running | <-- Where this is in green
stopped | <-- Where this is in red
***更新***解决方案:
red=$(tput setaf 1)
green=$(tput setaf 2)
normal=$(tput sgr0)
if [ $process_state != "running" ]; then
process_state="${red} $process_state ${normal}"
else
process_state="${green} $process_state ${normal}"
fi
printf "%s%-10s%s|" $process_state
注意:注意 $process_state 周围的空格将其与颜色分开。