-->

PS1环境变量无法在Mac上工作(PS1 env variable does not work on

2019-07-29 06:42发布

我有一个脚本(不是我自己写的),这显示了在我的命令提示符下git的分支/ SVN分支。 没有人知道为什么这不会在Mac上工作? 它完美的Linux操作系统。

从https://github.com/xumingming/dotfiles/blob/master/.ps1 :

# Display ps1 with colorful pwd and git status
# Acording to Jimmyxu .bashrc
# Modified by Ranmocy
# --

if type -P tput &>/dev/null && tput setaf 1 &>/dev/null; then
    color_prompt=yes
else
    color_prompt=
fi

__repo () {
    branch=$(type __git_ps1 &>/dev/null && __git_ps1 | sed -e "s/^ (//" -e "s/)$//")
    if [ "$branch" != "" ]; then
        vcs=git
    else
        branch=$(type -P hg &>/dev/null && hg branch 2>/dev/null)
        if [ "$branch" != "" ]; then
            vcs=hg
        elif [ -e .bzr ]; then
            vcs=bzr
        elif [ -e .svn ]; then
            vcs=svn
        else
            vcs=
        fi
    fi
    if [ "$vcs" != "" ]; then
        if [ "$branch" != "" ]; then
            repo=$vcs:$branch
        else
            repo=$vcs
        fi
        echo -n "($repo)"
    fi
    return 0
}

if [ "$color_prompt" = yes ]; then
# PS1='\[\e[01;32m\]\u@\h\[\e[00m\]:\[\e[01;34m\]\w\[\e[33;40m\]$(__repo)\[\e[00m\]\$ '
    PS1='\[\e[01;32m\]\u\[\e[00m\]:\[\e[01;34m\]\W\[\e[33m\]$(__repo)\[\e[00m\]\$ '
else
    PS1='\u@\h:\w$(__repo)\$ '
fi
unset color_prompt

case "$TERM" in
xterm*|rxvt*)
  PS1="\[\e]0;\W\a\]$PS1"
  ;;
*)
  ;;
esac

Answer 1:

混帐的Mac OS X的安装没有__git_ps1包括在内。

使用:

alias __git_ps1="git branch 2>/dev/null | grep '*' | sed 's/* \(.*\)/(\1)/'"

作为的取代。



Answer 2:

您提供的脚本未能检测混帐回购协议,如果该命令__git_ps1失败。 这个添加到~/.bash_profile

source /usr/local/git/contrib/completion/git-completion.bash
source /usr/local/git/contrib/completion/git-prompt.sh

假设你存储脚本文件~/.ps1 ,还可以添加:

source ~/.ps1

  • 该解决方案还使标签完成的饭桶。
  • 混帐的Mac OS X的安装 有__git_ps1在内,感谢sschuberth和cheapener用于提混帐completion.bash。


Answer 3:

在新的约塞米蒂MAC使用内置的混帐,我用这个:

source /Library/Developer/CommandLineTools/usr/share/git-core/git-completion.bash
source /Library/Developer/CommandLineTools/usr/share/git-core/git-prompt.sh
export PS1='\[\e]0;\u@\h: \w\a\]\[\e[32;1m\]\u@\h:\w \[\e[33;1m\]$(__git_ps1 "[%s] ")\[\e[32;1m\]\$ \[\e[0m\]'

注:在埃尔卡皮坦,我不得不改变混帐脚本的路径/Applications/Xcode.app/Contents/Developer/usr/share/git-core ,我想你必须有安装的XCode这个工作。



Answer 4:

如果通过使用MacPorts(GIT核心)安装的git,你应该添加下列到~/.bash_profile

source /opt/local/etc/profile.d/bash_completion.sh
source /opt/local/share/git-core/git-prompt.sh  

该git-prompt.sh的位置似乎已经改变了几次。



文章来源: PS1 env variable does not work on mac
标签: git macros ps1