macos - 如何配置 git 以继承我特定的 $PATH 环境变量?

标签 macos git bash emacs

我的目标是在我拥有的许多 shell 帐户之间共享一组点文件,以便我可以在每个帐户中拥有相似的环境。因此,我不想在我的 .git 文件或我的 .bash 文件中包含机器/环境特定信息。

在我的 MacOSX 机器上,默认的 emacs 版本是 22。我在 /usr/local/bin 中安装了更新版本的 emacs (24)。在我的 .bashrc 文件中,我将默认编辑器设置为 emacs:export EDITOR=emacs。在此之前,我还设置了我的路径,使 /usr/local/bin 出现在 /usr/bin 之前,以便选择较新版本的 emacs。

当我使用 git 创建提交消息时,它总是选择 /usr/bin 中的那个,而不是 /usr/local/bin 中的那个。 Git 忽略了我的 $PATH 设置。我曾经在我的 .gitconfigure 文件中硬编码我想要的 emacs 版本的位置,但是当我在我的 Ubuntu 服务器上使用相同的 .gitconfigure 文件时,这将不起作用。 (如果不清楚,我的意思是修改我的 .gitconfigure 编辑器选项以指向硬编码对我来说不起作用)。

为什么 git 不遵守我的 $PATH 设置?

如何配置 git 以使用正确的 emacs?


编辑:添加更多上下文

有人建议将环境变量放在 .profile/.bash_profile 中。这是我的 .bashrc.bash_profile

中的一些相关代码

.bashrc

注意:我编写了一个名为 vercomp 的 bash 函数来比较版本号。

OS=''                                                                                                
case $OSTYPE in                                                                                      
  darwin*)  OS='Mac' ;;                                                                              
  linux*)   OS='Linux'  ;;                                                                           
  *)        OS='UNKNOWN' ;;                                                                          
esac                                                                                                 

export EDITOR=emacs 

if [[ $OS = 'Mac' ]]; then                                                                           
    ### EMACS VERSION CHECK                                                                          
    # make sure that we're working with emacs >= 24                                                  
    wanted_ver=24                                                                                    
    curr_ver=`emacs --version | grep -oE '[[:digit:]]+\.[.[:digit:]]*'`                              
    vercomp $curr_ver $wanted_ver                                                                    

    # If vercomp returns 2, then our current emacs version isn't good enough.                        
    if [[ $? == 2 ]]; then                                                                           
        if [[ -e '/usr/local/bin/emacs' ]]; then                                                     
            emacs_path='/usr/local/bin/emacs -nw'                                                    
        elif  [[ -e '/Applications/Emacs.app/Contents/MacOS/Emacs' ]]; then                          
            emacs_path='/Applications/Emacs.app/Contents/MacOS/Emacs -nw'                            
        else                                                                                         
            echo -n "EMACS VERSION OUT OF DATE: $curr_emacs_version. "                               
            echo 'Install a newer version.'                                                          
            emacs_path=''                                                                            
        fi                                                                                           
        export EDITOR="$emacs_path"                                                                  
        alias emacs="$emacs_path"                                                                    
    fi                                                                                               
fi 

.bash_profile

source ~/.bashrc                                                                                     
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell         
session *as a function*                                                                              
LESS=-FRX;export LESS  

最佳答案

I expect to have an answer that explains WHY MacOSX doesn't pick up my bashrc/profile environment changes and a solution to HOW I can get MacOSX to pick up those changes.

这不是特定于 OS X - 这是一个例子:

$ uname -a
Linux hostname 3.13.7-gentoo #1 SMP PREEMPT Mon Mar 24 18:40:46 CET 2014 i686 Intel(R) Atom(TM) CPU 330 @ 1.60GHz GenuineIntel GNU/Linux
$ cat .bashrc
export EDITOR="emacs"
$ echo $EDITOR
/usr/bin/vim
$ mv .bashrc .bash_profile
$ logout
Connection to hostname closed.
$ ssh hostname
$ echo $EDITOR
emacs

就我所知并习惯于“存在”的是,环境变量应该驻留在 /etc/profile (系统范围)内,并且可以在用户级别的基础上扩展和覆盖 ~/.profile 分别为 ~/.bash_profile.

来自 man bash :

When bash is invoked as an interactive login shell, or as a non-interac- tive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.

另一方面。 .bashrc 是放置自定义函数、别名、提示配置等的地方。

因此,对于您的特定问题,一个简单的解决方案 - 除了在您的特殊情况下使用另一个 git 版本 - 只需将您的环境变量从 .bashrc 移动到 .bash_profile.

Linux From Scratch 项目提供了一些非常好的 examples about how to organize the bash shell startup files .内部解决了 OS X 上的特定问题 "Setting environment variables in OS X?" .

关于macos - 如何配置 git 以继承我特定的 $PATH 环境变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22474054/

相关文章:

python - 一种在 Linux 和 OS X 操作系统上获取用户安装包路径的方法? (适用于 2.5 - 2.7 之间的 Python 版本)

git - 如何搜索 git 存储库历史记录以查找 merge 错误?

regex - 从遵循 bash 脚本模式的单行中删除主机名

git - 从远程 Git 存储库中删除物理删除文件的引用

linux - 使用 ssh 和 NAT 运行 git 服务器

bash - 如何修复 "bash: flutter: command not found"错误?

bash:迭代目录内容直到条件匹配的最佳实践

python - 在 virtualenv 中运行 homebrew

java - MacOS X Big Sur 上的 Netbeans 没有找到 JDK

linux - MAC OS X 别名文件夹/文件不会在 Windows 上保留