准备git_ps1
这样的:
git_ps1=$(git branch 2>/dev/null | grep '*')
git_ps1="${git_ps1:+ (${git_ps1/#\* /}) }"
## ^space ^space
## Change the spaces if you like it other ways.
git_ps1
会像(master)
,两端有空格;和一个完整的空字符串 when git_ps1
is not set (for non-git dir) 。现在您可以使用该$git_ps1
变量将其插入到您喜欢的任何位置。
解释:
git_ps1="${git_ps1:+ (${git_ps1/#\* /}) }"
是条件变量赋值。
如果git_ps1
设置了,那么它将等于(${git_ps1/#\* /})
,否则它将保持为空。
${git_ps1/#\* /}
从开头切掉*
and 空间$git_ps1
示例用法:
__git_ps1(){
git_ps1=$(git branch 2>/dev/null | grep '*')
git_ps1="${git_ps1:+ (${git_ps1/#\* /})}"
echo "$git_ps1"
}
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[00m\]\[\033[01;34m\]\w\[\033[00m\]$(__git_ps1) \[\033[1;32m\]>\[\033[00m\]\[\033[1;32m\]>\[\033[00m\]\[\033[1;32m\]>\[\033[00m\] '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
这给了我这样的提示:
![在此处输入图像描述](https://i.stack.imgur.com/DilGO.png)