Mac OS 怎么修改 PATHS 环境变量

闲着没事儿蛋疼了一把,照着网上的教程搭建 vim IDE,需要修改一下 /etc/paths 这个文件

本以为改一下很简单的事情,结果用 TextEdit 打开硬是改不了,被锁住了。。

记得之前是直接在里面加过一行路径的,时隔已久现在已经想不不起来是怎么办到的了~

于是在网上找了写资料。。。

按照 “How To Edit Your PATH Environment Variables On Mac OS X” 写的试了一下,

在 Terminal 里面执行 echo $PATH 命令的时候发现,paths 文件里面在顶部多出了一行:

/Users/user/.rbenv/bin

很显然这个只是例子里面的示例路径,对我来说没什么用。。

好吧那我还是给删掉,结果发现没写怎么删掉,于是在去网上查,查到一条命令我试了一下:

unset PATH

执行完毕后我在执行 echo $PATH 查看效果怎么样了,

结果发现毛都没了,所有的环境变量都给删除掉了,

连续执行了几次 echo $PATH 命令,退出 Terminal 后再进,在执行 echo $PATH

发现不是眼花,所有的环境变量实实在在地都被删除掉了!- -、

这个最直接的后过就是一些命令用不了了,试了一下,发现果然不出我所料:

touch、open 这些命令都执行不了了,这样的话,按照 “How To Edit” 这篇文章里面的命令来敲的话已经没用了。

毫无头绪了好一会儿,想到我还是自己去几个集中放置 command line tool 的目录下去找一找这几个程序,

mac 的这几个目录还是比较好找,果然在 /usr/bin 里面找到了 touch、open 字样

于是在 Terminal 中先切换到 /usr/bin/ 目录:cd /usr/bin/

再执行 touch ~/.bash_profile; open ~/.bash_profile,没有再报出找不到该命令的信息了

所幸我之前备份了一下 /etc/paths 文件中的内容,这下就排上用场了

我在 .bash_profile 中加入了以下的内容:

export PATH="/usr/local/bin;/usr/bin;/bin;/usr/sbin;/sbin;/Users/user/Software/MacOS/RAR for Mac OS X V4.10;/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin;/Users/user/MyTools/EncodingConvertor;/Users/user/.local/bin"
然后执行 source ~/bash_profile

然后我很自信的执行了一下 touch,结果又被打击到了。。。
echo  $PATH 一看,尼玛所有环境变量全部被空格隔开了,

Mac OS 怎么修改 PATHS 环境变量_第1张图片

我隐约地记得我在动手之前正常的不是以这种格式显示的,有一种排列地很紧密的感觉~

Mac OS 怎么修改 PATHS 环境变量_第2张图片

重新回忆了一会儿,想起以前看过的文档里面写的是,mac 里面常用的分隔符是 冒号 而不是 分号,

这样的话,问题的症结就算是找到了,在 TextEdit 里面将所有 “;” 替换为 “:”,

export PATH="/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Users/user/Software/MacOS/RAR for Mac OS X V4.10:/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Users/user/MyTools/EncodingConvertor:/Users/user/.local/bin"

然后再重复上面所说的步骤,完了以后再次自信地执行 touch,发现已经不报出找不到 touch 命令的提示了

至此问题解决!此次给我的教训就是不要胡搞,能够修复自然是没问题,万一是不可逆的就完蛋了。。 


How To Edit Your PATH Environment Variables On Mac OS X

原文链接:http://hathaway.cc/2008/06/how-to-edit-your-path-environment-variables-on-mac-os-x/

If you are new to Mac OS X, you may need to know how to edit your PATH. The good news is that this is an easy task on Mac OS X.

The recommended way is by editing your .bash_profile file. This file is read and the commands in it executed by Bash every time you log in to the system. The best part is that this file is specific to your user so you won’t affect other users on the same system by changing it.

Step 1: Open up a Terminal window (this is in your Applications/Utilites folder by default)

Step 2: Enter the follow commands:

touch ~/.bash_profile; open ~/.bash_profile

This will open the .bash_profile file in Text Edit (the default text editor included on your system). The file allows you to customize the environment your user runs in.

Step 3: Add the following line to the end of the file adding whatever additional directory you want in your path:

export PATH="$HOME/.rbenv/bin:$PATH"

That example would add ~/.rbenv to the PATH. The $PATH part is important as it appends the existing PATH to preserve it in the new value.

Step 4: Save the .bash_profile file and Quit (Command + Q) Text Edit.

Step 5: Force the .bash_profile to execute. This loads the values immediately without having to reboot. In your Terminal window, run the following command.

source ~/.bash_profile

That’s it! Now you know how to edit the PATH on your Mac OS X computer system. You can confirm the new path by opening a new Terminal windows and running:

echo $PATH

You should now see the values you want in your PATH.

[Updated 2/10/2012] The instructions now use the .bash_profile method of editing your PATH. This is preferred as it keeps the changes specific to your user. I also updated the instructions to use Text Edit instead of vim so it is easier for a beginner.


你可能感兴趣的:(Mac OS 怎么修改 PATHS 环境变量)