因此,我有一个.bashrc
用于自定义提示的脚本(请参见下文)。
function git_unpushed {
brinfo=$(git branch -v)
if [[ $brinfo =~ ("[ahead "([[:digit:]]*)]) ]]
then
echo "Not Pushed: ${BASH_REMATCH[2]}"
fi
}
function git_untracked {
untracked=$(git clean --dry-run | wc -l)
if [ $untracked -gt 0 ]
then
echo "Untracked: "$untracked
fi
}
export PS1="\
$(
# last_result=$?
uid="$(id -u)"
host="\[\e[97m\]\H"
path="\[\e[94m\]\w"
# If root
if [ "$uid" = "0" ];
then
user="\[\e[95m\]\u"
symbol="\[\e[97m\]#"
else
# If not root
user="\[\e[96m\]\u"
symbol="\[\e[97m\]\$"
fi
# If Git Repo
if [ -d './.git' ];
then
unpushed=$(git_unpushed)
untracked=$(git_untracked)
branch=$(__git_ps1)
status=$(git diff --shortstat)
second_line="hi"
else
second_line=$path
fi
echo "\[\e[1m\]$user@$host\n$second_line\n$symbol: \[\e[0m\]"
)"
我的问题:为什么每次cd
转到git repo都不会替换路径?(如果我在回购库中启动bash提示,则会出现此情况”
我正在使用Ubuntu 14.04
经过大量工作使它正确无误,他就是我的结果:自定义$ PS1
谢谢大家的帮助!
正如@EtanReisner指出的那样,通过将命令替换括在单引号中,您的代码应该可以为所有用户使用。
export PS1='\
$(
# last_result=$?
uid="$(id -u)"
host="\[\e[97m\]\H"
path="\[\e[94m\]\w"
# If root
if [ "$uid" = "0" ];
then
user="\[\e[95m\]\u"
symbol="\[\e[97m\]#"
else
# If not root
user="\[\e[96m\]\u"
symbol="\[\e[97m\]\$"
fi
# If Git Repo
if [ -d "./.git" ];
then
unpushed=$(git_unpushed)
untracked=$(git_untracked)
branch=$(__git_ps1)
status=$(git diff --shortstat)
second_line="hi"
else
second_line=$path
fi
echo "\[\e[1m\]$user@$host\n$second_line\n$symbol: \[\e[0m\]"
)'
这是因为您要发生的事情仅在〜/ .bashrc每次获取源代码时才运行。要使它在执行每个命令后运行,可以创建一个函数并将环境变量PROMPT_COMMAND
设置为该函数。
试试这个:
new_ps1 (){
export PS1="\
$(
# last_result=$?
uid="$(id -u)"
host="\[\e[97m\]\H"
path="\[\e[94m\]\w"
# If root
if [ "$uid" = "0" ];
then
user="\[\e[95m\]\u"
symbol="\[\e[97m\]#"
else
# If not root
user="\[\e[96m\]\u"
symbol="\[\e[97m\]\$"
fi
# If Git Repo
if [ -d './.git' ];
then
unpushed=$(git_unpushed)
untracked=$(git_untracked)
branch=$(__git_ps1)
status=$(git diff --shortstat)
second_line="hi"
else
second_line=$path
fi
echo "\[\e[1m\]$user@$host\n$second_line\n$symbol: \[\e[0m\]"
)"
}
PROMPT_COMMAND="new_ps1"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句