【发布时间】:2017-08-01 09:14:15
【问题描述】:
为了帮助自己记住我登录到不同的系统,快速而直观,我为 Bash PS1 环境变量中的 \h 字段使用不同的颜色。
我正在寻找一种可靠地生成PS1 的方法,该方法与静态主机标识符相关联;比如说,通过hostid 命令。
原来,我的PS1,改编自bashrcgenerator.com生成的东西,看起来像:
export PS1="\u@\[$(tput bold)\]\[$(tput sgr0)\]\[\033[38;5;35m\]\h\[$(tput sgr0)\]\[$(tput sgr0)\]\[\033[38;5;15m\]\[$(tput sgr0)\]:\[\033[38;5;245m\]\w\[$(tput sgr0)\]\[\033[38;5;15m\]\[$(tput sgr0)\]\\$ "
真是一团糟。
所以要开始任何进展,第一步是进行一些重构。这让我看到了以下脚本:
bold="$(tput bold)"
reset="$(tput sgr0)"
green="\e[38;5;35m"
gray="\e[38;5;245m"
directory='\w'
host='\h'
user='\u'
function colorize() {
echo -n "${2}${1}${reset}"
}
export PS1="${user}@$(colorize $host $green):$(colorize $directory $gray)\\$ "
此时,您至少可以看到到底发生了什么。
现在,我需要编写如下函数:
get_repeatable_color_for_hostid() {
# hash the $(hostid) into a valid color escape string
# e.g. 16ab1d60 --> \e[38;5;35m
}
为此,我需要了解:
- 例如字段部分
xx;y;zzm的含义是什么?\e[38;5;35m? - 如何进行从
hostid到该颜色转义序列的散列,s.t.颜色尽可能随机化。
【问题讨论】:
标签: bash terminal xterm ps1 terminal-emulator