所以我发现了以下很酷的Bash提示:
..具有以下基本逻辑:
PS1="\[\033[01;37m\]\$? \$(if [[ \$? == 0 ]]; then echo \"\[\033[01;32m\]\342\234\223\"; else echo \"\[\033[01;31m\]\342\234\227\"; fi) $(if [[ ${EUID} == 0 ]]; then echo '\[\033[01;31m\]\h'; else echo '\[\033[01;32m\]\u@\h'; fi)\[\033[01;34m\] \w \$\[\033[00m\] "
Run Code Online (Sandbox Code Playgroud)
然而,这不是非常基本的,恰好是一个令人难以置信的混乱.我想让它更具可读性.
怎么样?
che*_*ner 15
用于PROMPT_COMMAND
以理智的方式建立价值.这样可以节省大量引用并使文本更具可读性.请注意,您可以使用\e
而不是\033
在提示中表示转义字符.
set_prompt () {
local last_command=$? # Must come first!
PS1=""
# Add a bright white exit status for the last command
PS1+='\[\e[01;37m\]$? '
# If it was successful, print a green check mark. Otherwise, print
# a red X.
if [[ $last_command == 0 ]]; then
PS1+='\[\e[01;32m\]\342\234\223 '
else
PS1+='\[\e[01;31m\]\342\234\227 '
fi
# If root, just print the host in red. Otherwise, print the current user
# and host in green.
# in
if [[ $EUID == 0 ]]; then
PS1+='\[\e[01;31m\]\h '
else
PS1+='\[\e[01;32m\]\u@\h '
fi
# Print the working directory and prompt marker in blue, and reset
# the text color to the default.
PS1+='\[\e[01;34m\] \w \$\[\e[00m\] '
}
PROMPT_COMMAND='set_prompt'
Run Code Online (Sandbox Code Playgroud)
您可以为更深奥的转义序列定义变量,代价是在双引号内需要一些额外的转义,以适应参数扩展.
set_prompt () {
local last_command=$? # Must come first!
PS1=""
local blue='\[\e[01;34m\]'
local white='\[\e[01;37m\]'
local red='\[\e[01;31m\]'
local green='\[\e[01;32m\]'
local reset='\[\e[00m\]'
local fancyX='\342\234\227'
local checkmark='\342\234\223'
PS1+="$white\$? "
if [[ $last_command == 0 ]]; then
PS1+="$green$checkmark "
else
PS1+="$red$fancyX "
fi
if [[ $EUID == 0 ]]; then
PS1+="$red\\h "
else
PS1+="$green\\u@\\h "
fi
PS1+="$blue\\w \\\$$reset "
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3148 次 |
最近记录: |