When someone says "edit your .plist file" or "your .profile" or ".bash_profile" etc, this just confuses me. I have no idea where these files are, how to create them if I have to do that, etc, and also why there seem to be so many different ones (why? Do they do different things?)
当有人说“编辑你的。plist文件”或“你的。profile”或“。bash_profile“等等,这让我很困惑。我不知道这些文件在哪里,如果我必须这么做,如何创建它们,等等,以及为什么会有这么多不同的文件(为什么?他们做不同的事情吗?
So could someone please explain very patiently to a previous Windows user (wanting desperately to become more familiar with the pleasant if initially somewhat confusing OS X world) how to do this step by step?
那么,是否有人可以耐心地向以前的Windows用户解释一下(如果最初有点迷惑OS X世界,想要变得更加熟悉这个令人愉快的系统)如何一步一步来做呢?
I need the variables to be set both for GUI applications and command line applications, and at the moment it's for an ant script that needs the variables, but there will most likely be other needs as well.
我需要为GUI应用程序和命令行应用程序设置这些变量,目前它是一个需要变量的ant脚本,但是很可能还有其他的需求。
Please note that I have Lion too, since many of the answers you get Googling seem to be outdated for Lion...
请注意,我也有狮子,因为你用谷歌搜索的很多答案似乎都过时了。
Also note that I have practically zero experience using the Terminal. I'm willing to learn, but please explain for a novice...
还要注意,我使用终端的经验几乎为零。我愿意学习,但是请给初学者解释一下……
15 个解决方案
#1
366
First, one thing to recognize about OS X is that it is built on Unix. This is where the .bash_profile comes in. When you start the Terminal app in OS X you get a bash shell by default. The bash shell comes from Unix and when it loads it runs the .bash_profile script. You can modify this script for your user to change your settings. This file is located at:
首先,关于OS X的一个认识是它是在Unix上构建的。这就是.bash_profile的所在。在OS X中启动终端应用程序时,默认情况下会得到一个bash shell。bash shell来自Unix,当它加载时,它运行.bash_profile脚本。您可以修改此脚本以供用户更改设置。该文件位于:
~/.bash_profile
Update for Mavericks
更新为小牛
OS X Mavericks does not use the environment.plist - at least not for OS X windows applications. You can use the launchd configuration for windowed applications. The .bash_profile is still supported since that is part of the bash shell used in Terminal.
OS X Mavericks不使用环境。plist -至少不适合OS X windows应用程序。您可以使用launchd配置来进行窗口应用。bash_profile仍然被支持,因为它是在终端使用的bash shell的一部分。
Lion and Mountain Lion Only
狮子和美洲狮。
OS X windowed applications receive environment variables from the your environment.plist file. This is likely what you mean by the ".plist" file. This file is located at:
OS X窗口应用程序从您的环境中接收环境变量。plist文件。这可能就是你所说的“。”plist文件”。该文件位于:
~/.MacOSX/environment.plist
If you make a change to your environment.plist file then OS X windows applications, including the Terminal app, will have those environment variables set. Any environment variable you set in your .bash_profile will only affect your bash shells.
如果你改变了你的环境。plist文件然后OS X windows应用程序,包括终端应用程序,将会设置这些环境变量。
Generally I only set variables in my .bash_profile file and don't change the .plist file (or launchd file on Mavericks). Most OS X windowed applications don't need any custom environment. Only when an application actually needs a specific environment variable do I change the environment.plist (or launchd file on Mavericks).
通常,我只在.bash_profile文件中设置变量,并且不更改.plist文件(或Mavericks的launchd文件)。大多数OS X窗口应用程序不需要任何自定义环境。只有当应用程序实际需要一个特定的环境变量时,我才会改变环境。plist(或Mavericks的launchd文件)。
It sounds like what you want is to change the environment.plist file, rather than the .bash_profile.
听起来你想要的是改变环境。plist文件,而不是.bash_profile。
One last thing, if you look for those files, I think you will not find them. If I recall correctly, they were not on my initial install of Lion.
最后一件事,如果你寻找那些文件,我想你不会找到它们。如果我没记错的话,它们并不是我最初安装的。
Edit: Here are some instructions for creating a plist file.
这里有一些创建plist文件的说明。
- Open Xcode
- 打开Xcode
- Select File -> New -> New File...
- 选择文件->新->新文件…
- Under Mac OS X select Resources
- 在Mac OS X下选择资源。
- Choose a plist file
- 选择一个plist文件
- Follow the rest of the prompts
- 跟随其他提示。
To edit the file, you can Control-click to get a menu and select Add Row. You then can add a key value pair. For environment variables, the key is the environment variable name and the value is the actual value for that environment variable.
要编辑该文件,可以控制单击以获取菜单并选择Add Row。然后可以添加一个键值对。对于环境变量,关键是环境变量名,值是该环境变量的实际值。
Once the plist file is created you can open it with Xcode to modify it anytime you wish.
一旦创建了plist文件,就可以用Xcode打开它,随时修改它。
#2
193
Your ".profile" or ".bash_profile" are simply files that are present in your "home" folder. If you open a Finder window and click your account name in the Favorites pane ... you won't see them. If you open a Terminal window and type "ls" to list files ... you still won't see them. But you can find them by using "ls -a" in the terminal. Or if you open your favorite text editor (say TextEdit since it comes with OS X) and do File->Open and then press Command+Shift+. and click on your account name (home folder) you will see them as well. If you do not see them, then you can simply create one in your favorite text editor.
你的”。配置文件”或“。bash_profile“只是存在于您的“home”文件夹中的文件。如果您打开Finder窗口并在Favorites窗格中单击您的帐户名称…你不会看到它们。如果您打开一个终端窗口并键入“ls”来列出文件…你还是看不到他们。但是你可以在终端使用“ls -a”找到它们。或者,如果你打开你喜欢的文本编辑器(比如TextEdit,因为它和OS X一起出现),然后打开文件->,然后按Command+Shift+。然后点击你的帐户名(家庭文件夹)你也会看到他们。如果您没有看到它们,那么您可以在您喜欢的文本编辑器中创建一个。
Now, adding environment variables is fairly straightforward and remarkably similar to windows conceptually. In your .profile just add, one per line, the variable name and its value as follows:
现在,添加环境变量非常简单,与windows概念上非常相似。在您的.profile中,只需添加一行,变量名及其值如下:
export JAVA_HOME=/Library/Java/Home
export JRE_HOME=/Library/Java/Home
etc.
等。
If you are modifying your "PATH" variable, be sure to include the system's default PATH that was already set for you:
如果您正在修改您的“PATH”变量,请确保包含系统的默认路径,该路径已经为您设置了:
export PATH=$PATH:/path/to/my/stuff
Now here is the quirky part, you can either open a new Terminal window to have the new variables take effect, or you will need to type ". .profile" or ". .bash_profile" to reload the file and have the contents be applied to your current Terminal's environment.
现在,这里有一个古怪的部分,您可以打开一个新的终端窗口,让新变量生效,或者您需要键入“.profile”或“.bash_profile”来重新加载文件,并将内容应用到当前终端的环境中。
You can check that your changes took effect using the "set" command in your Terminal. Just type "set" (or "set | more" if you prefer a paginated list) and be sure what you added to the file is there.
您可以在终端中使用“set”命令检查您的更改是否生效。只要键入“set”(或者“设置|”,如果你更喜欢一个分页列表),并确定添加到文件中的内容。
As for adding environment variables to GUI apps, that is normally not necessary and I'd like to hear more about what you are specifically trying to do to better give you an answer for it.
至于在GUI应用程序中添加环境变量,这通常是不必要的,我希望能听到更多关于您具体想做什么,以便更好地为您提供一个答案。
#3
74
Here's a bit more information specifically regarding the PATH variable in Lion OS 10.7.x:
这里有一些关于Lion OS 10.7.x中路径变量的详细信息:
If you need to set the PATH globally, the PATH is built by the system in the following order:
如果您需要在全局上设置路径,则系统按照以下顺序构建路径:
- Parsing the contents of the file
/private/etc/paths
, one path per line - 解析文件/私有/etc/路径的内容,每行一条路径。
- Parsing the contents of the folder
/private/etc/paths.d
. Each file in that folder can contain multiple paths, one path per line. Load order is determined by the file name first, and then the order of the lines in the file. - 解析文件夹/私有/etc/paths.d的内容。该文件夹中的每个文件都可以包含多条路径,每一行有一条路径。加载顺序由文件名首先确定,然后是文件中的行顺序。
- A
setenv PATH
statement in/private/etc/launchd.conf
, which will append that path to the path already built in #1 and #2 (you must not use $PATH to reference the PATH variable that has been built so far). But, setting the PATH here is completely unnecessary given the other two options, although this is the place where other global environment variables can be set for all users. - 一个setenv路径声明在/private/etc/launchd。conf将在#1和#2中所构建的路径中添加该路径(您不能使用$ path来引用已经构建的路径变量)。但是,给定其他两个选项,在这里设置路径是完全不必要的,尽管这是为所有用户设置其他全局环境变量的地方。
These paths and variables are inherited by all users and applications, so they are truly global -- logging out and in will not reset these paths -- they're built for the system and are created before any user is given the opportunity to login, so changes to these require a system restart to take effect.
这些路径和变量是继承了所有的用户和应用程序,所以它们是真正意义上的全球——注销,不会重置这些路径——他们建造之前创建的系统和用户有机会登录,所以改变这些需要重新启动系统才能生效。
BTW, a clean install of OS 10.7.x Lion doesn't have an environment.plist
that I can find, so it may work but may also be deprecated.
顺便说一下,一个完整的OS 10.7安装。狮子没有环境。我可以找到plist,所以它可能有用,但也可能被弃用。
#4
43
echo $PATH
it prints current path value
它打印当前路径值。
Then do vim ~/.bash_profile
and write
然后vim ~ /。bash_profile和写
export PATH=$PATH:/new/path/to/be/added
here you are appending to the old path, so preserves the old path and adds your new path to it
在这里,您将附加到旧路径,因此保留旧路径并添加您的新路径。
then do
然后做
source ~/.bash_profile
this will execute it and add the path
这将执行它并添加路径。
then again check with
再一次检查
echo $PATH
#5
30
Unfortunately none of these answers solved the specific problem I had.
不幸的是,这些答案都没有解决我的具体问题。
Here's a simple solution without having to mess with bash
. In my case, it was getting gradle
to work (for Android Studio
).
这里有一个简单的解决方案,而不需要使用bash。在我的例子中,它是让gradle工作(对于Android Studio)。
Btw, These steps relate to OSX (Mountain Lion 10.8.5)
顺便说一下,这些步骤与OSX有关(Mountain Lion 10.8.5)
- Open up Terminal.
- 打开终端。
-
Run the following command:
运行以下命令:
sudo nano /etc/paths
(orsudo vim /etc/paths
for vim)sudo nano /etc/paths(或sudo vim /etc/path for vim)
- Go to the bottom of the file, and enter the path you wish to add.
- 进入文件的底部,并输入您想要添加的路径。
- Hit control-x to quit.
- 打击control-x戒烟。
- Enter 'Y' to save the modified buffer.
- 输入'Y'以保存修改后的缓冲区。
-
Open a new terminal window then type:
打开一个新的终端窗口,然后输入:
echo $PATH
回声路径美元
You should see the new path appended to the end of the PATH
您应该看到在路径末尾添加的新路径。
I got these details from this post:
我从这篇文章中得到了这些细节:
http://architectryan.com/2012/10/02/add-to-the-path-on-mac-os-x-mountain-lion/#.UkED3rxPp3Q
http://architectryan.com/2012/10/02/add-to-the-path-on-mac-os-x-mountain-lion/ .UkED3rxPp3Q
I hope that can help someone else
我希望能帮助别人。
#6
23
Simplified Explanation
This post/question is kind of old, so I will answer a simplified version for OS X Lion users. By default, OSX Lion does not have any of the following files:
这个post/问题有点旧,所以我将回答OS X Lion用户的简化版本。默认情况下,OSX Lion没有以下文件:
- ~/.bashrc
- ~ / . bashrc
- ~/.bash_profile
- ~ / . bash_profile
- ~/.profile
- ~ / . profile
At most, if you've done anything in the terminal you might see ~/.bash_history
在大多数情况下,如果您在终端中做过任何事情,您可能会看到~/.bash_history。
What It Means
You must create the file to set your default bash commands (commonly in ~/.bashrc). To do this, use any sort of editor, though it's more simple to do it within the terminal:
您必须创建文件来设置您的默认bash命令(通常在~/.bashrc中)。要做到这一点,请使用任何类型的编辑器,尽管在终端中执行它更简单:
- %> emacs .profile
- % > emacs . profile
- [from w/in emacs type:]
source ~/.bashrc
- [from w/in emacs type:] source ~/.bashrc。
- [from w/in emacs type:]
Ctrl + x Ctrl + s
(to save the file) - [from w/in emacs type:] Ctrl + x Ctrl + s(保存文件)
- [from w/in emacs type:]
Ctrl + x Ctrl + c
(to close emacs) - [从emacs类型:]Ctrl + x Ctrl + c(关闭emacs)
- %> emacs .bashrc
- % > emacs . bashrc
- [from w/in emacs type/paste all your bash commands, save, and exit]
- [从emacs类型/粘贴所有您的bash命令,保存和退出]
The next time you quit and reload the terminal, it should load all your bash preferences. For good measure, it's usually a good idea to separate your commands into useful file names. For instance, from within ~/.bashrc, you should have a source ~/.bash_aliases
and put all your alias commands in ~/.bash_aliases.
下一次当您退出并重新加载终端时,它应该加载您所有的bash首选项。为了更好地度量,将命令分隔成有用的文件名通常是个好主意。例如,从内部~/。bashrc,你应该有一个源~/。bash_aliases并将所有别名命令放入~/.bash_aliases中。
#7
18
What worked for me is to create a .launchd.conf
with the variables I needed:
为我工作的是创建一个。launchd。使用我需要的变量:
setenv FOO barbaz
This file is read by launchd at login. You can add a variable 'on the fly' to the running launchd using:
这个文件在登录时被launchd读取。您可以在运行的launchd中添加一个变量“on the fly”:
launchctl setenv FOO barbaz`
In fact, .launchd.cond
simply contains launchctl commands.
事实上,.launchd。cond只包含launchctl命令。
Variables set this way seem to be present in GUI applications properly.
这样设置的变量似乎在GUI应用程序中是正确的。
If you happen to be trying to set your LANG or LC_ variables in this way, and you happen to be using iTerm2, make sure you disable the 'Set locale variables automatically' setting under the Terminal tab of the Profile you're using. That seems to override launchd's environment variables, and in my case was setting a broken LC_CTYPE causing issues on remote servers (which got passed the variable).
如果您正试图以这种方式设置您的LANG或LC_变量,并且碰巧使用iTerm2,请确保在您使用的概要文件的终端选项卡下禁用“设置区域设置变量”。这似乎覆盖了launchd的环境变量,在我的例子中是在远程服务器上设置了一个损坏的LC_CTYPE,从而导致了问题(通过了变量)。
(The environment.plist still seems to work on my Lion though. You can use the RCenvironment preference pane to maintain the file instead of manually editing it or required Xcode. Still seems to work on Lion, though it's last update is from the Snow Leopard era. Makes it my personally preferred method.)
(环境。不过,plist似乎仍在我的狮子身上发挥作用。您可以使用RCenvironment配置窗格来维护文件,而不是手工编辑它或需要Xcode。尽管它的最新更新来自雪豹时代,但它似乎仍然对狮子有效。使它成为我个人喜欢的方法。
#8
14
Setup your PATH environment variable on Mac OS
Open the Terminal program (this is in your Applications/Utilites folder by default). Run the following command
打开终端程序(默认情况下,这是在您的应用程序/使用文件夹中)。运行以下命令
touch ~/.bash_profile; open ~/.bash_profile
This will open the file in the your default text editor.
这将在您的默认文本编辑器中打开该文件。
For ANDROID SDK as example :
以ANDROID SDK为例:
You need to add the path to your Android SDK platform-tools and tools directory. In my example I will use "/Development/android-sdk-macosx" as the directory the SDK is installed in. Add the following line:
您需要为Android SDK平台工具和工具目录添加路径。在我的例子中,我将使用“/开发/android-sdk-macosx”作为SDK安装的目录。添加以下行:
export PATH=${PATH}:/Development/android-sdk-macosx/platform-tools:/Development/android-sdk-macosx/tools
Save the file and quit the text editor. Execute your .bash_profile to update your PATH.
保存文件并退出文本编辑器。执行.bash_profile以更新路径。
source ~/.bash_profile
Now everytime you open the Terminal program you PATH will included the Android SDK.
现在,每次打开终端程序时,都会包含Android SDK。
#9
11
Adding Path Variables to OS X Lion
This was pretty straight forward and worked for me, in terminal:
这是非常直接的,对我来说,在终端:
$echo "export PATH=$PATH:/path/to/whatever" >> .bash_profile #replace "/path/to/whatever" with the location of what you want to add to your bash profile, i.e: $ echo "export PATH=$PATH:/usr/local/Cellar/nginx/1.0.12/sbin" >> .bash_profile
$. .bash_profile #restart your bash shell
A similar response was here: http://www.mac-forums.com/forums/os-x-operating-system/255324-problems-setting-path-variable-lion.html#post1317516
类似的响应也出现在这里:http://www.mac-forums.com/forums/os-x-operating-system/255324-问题- settingpath- variable-lion.html#post1317516。
#10
6
Open Terminal:
打开终端:
vi ~/.bash_profile
Apply changing to system (no need restart computer):
应用变更到系统(不需要重新启动计算机):
source ~/.bash_profile
(Also work with macOS Sierra 10.12.1)
(还与macOS Sierra 10.12.1合作)
#11
5
I had problem with Eclipse (started as GUI, not from script) on Maverics that it did not take custom PATH. I tried all the methods mentioned above to no avail. Finally I found the simplest working answer based on hints from here:
我遇到了Eclipse(从GUI开始,不是从脚本开始)的问题,因为它没有采用自定义路径。我尝试了上面提到的所有方法都无济于事。最后,我根据这里的提示找到了最简单的工作回答:
-
Go to /Applications/eclipse/Eclipse.app/Contents folder
去/应用程序/ eclipse / eclipse。应用程序/内容文件夹
-
Edit Info.plist file with text editor (or XCode), add LSEnvironment dictionary for environment variable with full path. Note that it includes also /usr/bin etc:
编辑信息。使用文本编辑器(或XCode)的plist文件,为环境变量添加完整路径的LSEnvironment字典。注意,它还包括/usr/bin等:
<dict> <key>LSEnvironment</key> <dict> <key>PATH</key> <string>/usr/bin:/bin:/usr/sbin:/sbin:/dev/android-ndk-r9b</string> </dict> <key>CFBundleDisplayName</key> <string>Eclipse</string> ...
-
Reload parameters for app with
应用程序的重载参数。
/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -v -f /Applications/eclipse/Eclipse.app
-
Restart Eclipse
重新启动Eclipse
#12
4
I took the idiot route. Added these to the end of /etc/profile
我选择了白痴路线。将这些添加到/etc/profile的末尾。
for environment in `find /etc/environments.d -type f`
do
. $environment
done
created a folder /etc/environments create a file in it called "oracle" or "whatever" and added the stuff I needed set globally to it.
创建一个文件夹/etc/environment创建一个名为“oracle”或“whatever”的文件,并添加我需要在全局上设置的内容。
/etc$ cat /etc/environments.d/Oracle
猫/etc/environments.d美元/ etc / Oracle
export PATH=$PATH:/Library/Oracle/instantclient_11_2
export DYLD_LIBRARY_PATH=/Library/Oracle/instantclient_11_2
export SQLPATH=/Library/Oracle/instantclient_11_2
export PATH=$PATH:/Library/Oracle/instantclient_11_2
export TNS_ADMIN=/Library/Oracle/instantclient_11_2/network/admin
#13
3
Let me illustrate you from my personal example in a very redundant way.
让我用一种非常多余的方式来说明我个人的例子。
- First after installing JDK, make sure it's installed.
- 在安装JDK之后,首先要确保安装了JDK。
-
Sometimes macOS or Linux automatically sets up environment variable for you unlike Windows. But that's not the case always. So let's check it. The line immediately after echo $JAVA_HOME would be empty if the environment variable is not set. It must be empty in your case.
有时macOS或Linux会自动为您设置环境变量,而不是Windows。但事实并非如此。让我们检查一下。如果环境变量没有设置,那么echo $JAVA_HOME后面的行将是空的。在您的情况下,它必须是空的。
-
Now we need to check if we have bash_profile file. You saw that in my case we already have bash_profile. If not we have to create a bash_profile file.
现在我们需要检查是否有bash_profile文件。在我的例子中,我们已经有bash_profile。如果不是,我们必须创建bash_profile文件。
-
创建一个bash_profile文件。
-
Check again to make sure bash_profile file is there.
再次检查以确保bash_profile文件在那里。
-
Now let's open bash_profile file. macOS opens it using it's default TextEdit program.
现在让我们打开bash_profile文件。macOS使用它的默认TextEdit程序打开它。
-
This is the file where environment variables are kept. If you have opened a new bash_profile file, it must be empty. In my case, it was already set for python programming language and Anaconda distribution. Now, i need to add environment variable for Java which is just adding the first line. YOU MUST TYPE the first line VERBATIM. JUST the first line. Save and close the TextEdit. Then close the terminal.
这是保存环境变量的文件。如果您已经打开了一个新的bash_profile文件,那么它必须是空的。在我的例子中,它已经设置为python编程语言和Anaconda分布。现在,我需要为Java添加环境变量,它只是添加了第一行。你必须逐字输入第一行。第一行。保存并关闭TextEdit。然后关闭终端。
-
Open the terminal again. Let's check if the environment variable is set up.
再次打开终端。让我们检查一下环境变量是否设置好了。
#14
1
More detail, which may perhaps be helpful to someone:
更多的细节,也许对某人有帮助:
Due to my own explorations, I now know how to set environment variables in 7 of 8 different ways. I was trying to get an envar through to an application I'm developing under Xcode. I set "tracer" envars using these different methods to tell me which ones get it into the scope of my application. From the below, you can see that editing the "scheme" in Xcode to add arguments works, as does "putenv". What didn't set it in that scope: ~/.MACOS/environment.plist, app-specific plist, .profile, and adding a build phase to run a custom script (I found another way in Xcode [at least] to set one but forgot what I called the tracer and can't find it now; maybe it's on another machine....)
由于我自己的探索,我现在知道如何用8种不同的方式来设置环境变量。我试图将envar应用到Xcode中开发的应用程序中。我用这些不同的方法设置了“tracer”envars,告诉我哪些方法可以进入我的应用程序的范围。从下面,您可以看到在Xcode中编辑“scheme”来添加参数,就像“putenv”一样。在这个范围内没有设置的是:~/. macos /环境。plist,应用程序特定的plist, .profile,并添加一个构建阶段来运行一个自定义脚本(我在Xcode中找到了另一种方法[至少]设置一个,但是忘记了我所称的跟踪器,现在找不到它;可能是在另一台机器上
GPU_DUMP_DEVICE_KERNEL is 3
GPU_DUMP_DEVICE_KERNEL是3
GPU_DUMP_TRK_ENVPLIST is (null)
GPU_DUMP_TRK_ENVPLIST(空)
GPU_DUMP_TRK_APPPLIST is (null)
GPU_DUMP_TRK_APPPLIST(空)
GPU_DUMP_TRK_DOTPROFILE is (null)
GPU_DUMP_TRK_DOTPROFILE(空)
GPU_DUMP_TRK_RUNSCRIPT is (null)
GPU_DUMP_TRK_RUNSCRIPT(空)
GPU_DUMP_TRK_SCHARGS is 1
GPU_DUMP_TRK_SCHARGS是1
GPU_DUMP_TRK_PUTENV is 1
GPU_DUMP_TRK_PUTENV是1
... on the other hand, if I go into Terminal and say "set", it seems the only one it gets is the one from .profile (I would have thought it would pick up environment.plist also, and I'm sure once I did see a second tracer envar in Terminal, so something's probably gone wonky since then. Long day....)
…另一方面,如果我进入终端并说“set”,它似乎是唯一一个从。profile(我本以为它会选择环境)的。plist也是,我确定一旦我在终端看到了第二个tracer envar,那么从那以后可能就会变得不稳定了。漫长的一天....)
#15
-3
Step1: open ~/.bash_profile
步骤1:打开~ / . bash_profile
Now a text editor opens:
现在,一个文本编辑器打开:
Step2: variable name should be in capitals. in this example variable is NODE_ENV
步骤2:变量名应该大写。在这个例子中变量是NODE_ENV。
Step3: export NODE_ENV=development
步骤3:出口NODE_ENV =发展
Save it and close.
保存并关闭。
Restart your system.
重新启动您的系统。
Done.
完成了。
To check env variable: open terminal and type
检查env变量:打开终端和类型。
echo $NODE_ENV
#1
366
First, one thing to recognize about OS X is that it is built on Unix. This is where the .bash_profile comes in. When you start the Terminal app in OS X you get a bash shell by default. The bash shell comes from Unix and when it loads it runs the .bash_profile script. You can modify this script for your user to change your settings. This file is located at:
首先,关于OS X的一个认识是它是在Unix上构建的。这就是.bash_profile的所在。在OS X中启动终端应用程序时,默认情况下会得到一个bash shell。bash shell来自Unix,当它加载时,它运行.bash_profile脚本。您可以修改此脚本以供用户更改设置。该文件位于:
~/.bash_profile
Update for Mavericks
更新为小牛
OS X Mavericks does not use the environment.plist - at least not for OS X windows applications. You can use the launchd configuration for windowed applications. The .bash_profile is still supported since that is part of the bash shell used in Terminal.
OS X Mavericks不使用环境。plist -至少不适合OS X windows应用程序。您可以使用launchd配置来进行窗口应用。bash_profile仍然被支持,因为它是在终端使用的bash shell的一部分。
Lion and Mountain Lion Only
狮子和美洲狮。
OS X windowed applications receive environment variables from the your environment.plist file. This is likely what you mean by the ".plist" file. This file is located at:
OS X窗口应用程序从您的环境中接收环境变量。plist文件。这可能就是你所说的“。”plist文件”。该文件位于:
~/.MacOSX/environment.plist
If you make a change to your environment.plist file then OS X windows applications, including the Terminal app, will have those environment variables set. Any environment variable you set in your .bash_profile will only affect your bash shells.
如果你改变了你的环境。plist文件然后OS X windows应用程序,包括终端应用程序,将会设置这些环境变量。
Generally I only set variables in my .bash_profile file and don't change the .plist file (or launchd file on Mavericks). Most OS X windowed applications don't need any custom environment. Only when an application actually needs a specific environment variable do I change the environment.plist (or launchd file on Mavericks).
通常,我只在.bash_profile文件中设置变量,并且不更改.plist文件(或Mavericks的launchd文件)。大多数OS X窗口应用程序不需要任何自定义环境。只有当应用程序实际需要一个特定的环境变量时,我才会改变环境。plist(或Mavericks的launchd文件)。
It sounds like what you want is to change the environment.plist file, rather than the .bash_profile.
听起来你想要的是改变环境。plist文件,而不是.bash_profile。
One last thing, if you look for those files, I think you will not find them. If I recall correctly, they were not on my initial install of Lion.
最后一件事,如果你寻找那些文件,我想你不会找到它们。如果我没记错的话,它们并不是我最初安装的。
Edit: Here are some instructions for creating a plist file.
这里有一些创建plist文件的说明。
- Open Xcode
- 打开Xcode
- Select File -> New -> New File...
- 选择文件->新->新文件…
- Under Mac OS X select Resources
- 在Mac OS X下选择资源。
- Choose a plist file
- 选择一个plist文件
- Follow the rest of the prompts
- 跟随其他提示。
To edit the file, you can Control-click to get a menu and select Add Row. You then can add a key value pair. For environment variables, the key is the environment variable name and the value is the actual value for that environment variable.
要编辑该文件,可以控制单击以获取菜单并选择Add Row。然后可以添加一个键值对。对于环境变量,关键是环境变量名,值是该环境变量的实际值。
Once the plist file is created you can open it with Xcode to modify it anytime you wish.
一旦创建了plist文件,就可以用Xcode打开它,随时修改它。
#2
193
Your ".profile" or ".bash_profile" are simply files that are present in your "home" folder. If you open a Finder window and click your account name in the Favorites pane ... you won't see them. If you open a Terminal window and type "ls" to list files ... you still won't see them. But you can find them by using "ls -a" in the terminal. Or if you open your favorite text editor (say TextEdit since it comes with OS X) and do File->Open and then press Command+Shift+. and click on your account name (home folder) you will see them as well. If you do not see them, then you can simply create one in your favorite text editor.
你的”。配置文件”或“。bash_profile“只是存在于您的“home”文件夹中的文件。如果您打开Finder窗口并在Favorites窗格中单击您的帐户名称…你不会看到它们。如果您打开一个终端窗口并键入“ls”来列出文件…你还是看不到他们。但是你可以在终端使用“ls -a”找到它们。或者,如果你打开你喜欢的文本编辑器(比如TextEdit,因为它和OS X一起出现),然后打开文件->,然后按Command+Shift+。然后点击你的帐户名(家庭文件夹)你也会看到他们。如果您没有看到它们,那么您可以在您喜欢的文本编辑器中创建一个。
Now, adding environment variables is fairly straightforward and remarkably similar to windows conceptually. In your .profile just add, one per line, the variable name and its value as follows:
现在,添加环境变量非常简单,与windows概念上非常相似。在您的.profile中,只需添加一行,变量名及其值如下:
export JAVA_HOME=/Library/Java/Home
export JRE_HOME=/Library/Java/Home
etc.
等。
If you are modifying your "PATH" variable, be sure to include the system's default PATH that was already set for you:
如果您正在修改您的“PATH”变量,请确保包含系统的默认路径,该路径已经为您设置了:
export PATH=$PATH:/path/to/my/stuff
Now here is the quirky part, you can either open a new Terminal window to have the new variables take effect, or you will need to type ". .profile" or ". .bash_profile" to reload the file and have the contents be applied to your current Terminal's environment.
现在,这里有一个古怪的部分,您可以打开一个新的终端窗口,让新变量生效,或者您需要键入“.profile”或“.bash_profile”来重新加载文件,并将内容应用到当前终端的环境中。
You can check that your changes took effect using the "set" command in your Terminal. Just type "set" (or "set | more" if you prefer a paginated list) and be sure what you added to the file is there.
您可以在终端中使用“set”命令检查您的更改是否生效。只要键入“set”(或者“设置|”,如果你更喜欢一个分页列表),并确定添加到文件中的内容。
As for adding environment variables to GUI apps, that is normally not necessary and I'd like to hear more about what you are specifically trying to do to better give you an answer for it.
至于在GUI应用程序中添加环境变量,这通常是不必要的,我希望能听到更多关于您具体想做什么,以便更好地为您提供一个答案。
#3
74
Here's a bit more information specifically regarding the PATH variable in Lion OS 10.7.x:
这里有一些关于Lion OS 10.7.x中路径变量的详细信息:
If you need to set the PATH globally, the PATH is built by the system in the following order:
如果您需要在全局上设置路径,则系统按照以下顺序构建路径:
- Parsing the contents of the file
/private/etc/paths
, one path per line - 解析文件/私有/etc/路径的内容,每行一条路径。
- Parsing the contents of the folder
/private/etc/paths.d
. Each file in that folder can contain multiple paths, one path per line. Load order is determined by the file name first, and then the order of the lines in the file. - 解析文件夹/私有/etc/paths.d的内容。该文件夹中的每个文件都可以包含多条路径,每一行有一条路径。加载顺序由文件名首先确定,然后是文件中的行顺序。
- A
setenv PATH
statement in/private/etc/launchd.conf
, which will append that path to the path already built in #1 and #2 (you must not use $PATH to reference the PATH variable that has been built so far). But, setting the PATH here is completely unnecessary given the other two options, although this is the place where other global environment variables can be set for all users. - 一个setenv路径声明在/private/etc/launchd。conf将在#1和#2中所构建的路径中添加该路径(您不能使用$ path来引用已经构建的路径变量)。但是,给定其他两个选项,在这里设置路径是完全不必要的,尽管这是为所有用户设置其他全局环境变量的地方。
These paths and variables are inherited by all users and applications, so they are truly global -- logging out and in will not reset these paths -- they're built for the system and are created before any user is given the opportunity to login, so changes to these require a system restart to take effect.
这些路径和变量是继承了所有的用户和应用程序,所以它们是真正意义上的全球——注销,不会重置这些路径——他们建造之前创建的系统和用户有机会登录,所以改变这些需要重新启动系统才能生效。
BTW, a clean install of OS 10.7.x Lion doesn't have an environment.plist
that I can find, so it may work but may also be deprecated.
顺便说一下,一个完整的OS 10.7安装。狮子没有环境。我可以找到plist,所以它可能有用,但也可能被弃用。
#4
43
echo $PATH
it prints current path value
它打印当前路径值。
Then do vim ~/.bash_profile
and write
然后vim ~ /。bash_profile和写
export PATH=$PATH:/new/path/to/be/added
here you are appending to the old path, so preserves the old path and adds your new path to it
在这里,您将附加到旧路径,因此保留旧路径并添加您的新路径。
then do
然后做
source ~/.bash_profile
this will execute it and add the path
这将执行它并添加路径。
then again check with
再一次检查
echo $PATH
#5
30
Unfortunately none of these answers solved the specific problem I had.
不幸的是,这些答案都没有解决我的具体问题。
Here's a simple solution without having to mess with bash
. In my case, it was getting gradle
to work (for Android Studio
).
这里有一个简单的解决方案,而不需要使用bash。在我的例子中,它是让gradle工作(对于Android Studio)。
Btw, These steps relate to OSX (Mountain Lion 10.8.5)
顺便说一下,这些步骤与OSX有关(Mountain Lion 10.8.5)
- Open up Terminal.
- 打开终端。
-
Run the following command:
运行以下命令:
sudo nano /etc/paths
(orsudo vim /etc/paths
for vim)sudo nano /etc/paths(或sudo vim /etc/path for vim)
- Go to the bottom of the file, and enter the path you wish to add.
- 进入文件的底部,并输入您想要添加的路径。
- Hit control-x to quit.
- 打击control-x戒烟。
- Enter 'Y' to save the modified buffer.
- 输入'Y'以保存修改后的缓冲区。
-
Open a new terminal window then type:
打开一个新的终端窗口,然后输入:
echo $PATH
回声路径美元
You should see the new path appended to the end of the PATH
您应该看到在路径末尾添加的新路径。
I got these details from this post:
我从这篇文章中得到了这些细节:
http://architectryan.com/2012/10/02/add-to-the-path-on-mac-os-x-mountain-lion/#.UkED3rxPp3Q
http://architectryan.com/2012/10/02/add-to-the-path-on-mac-os-x-mountain-lion/ .UkED3rxPp3Q
I hope that can help someone else
我希望能帮助别人。
#6
23
Simplified Explanation
This post/question is kind of old, so I will answer a simplified version for OS X Lion users. By default, OSX Lion does not have any of the following files:
这个post/问题有点旧,所以我将回答OS X Lion用户的简化版本。默认情况下,OSX Lion没有以下文件:
- ~/.bashrc
- ~ / . bashrc
- ~/.bash_profile
- ~ / . bash_profile
- ~/.profile
- ~ / . profile
At most, if you've done anything in the terminal you might see ~/.bash_history
在大多数情况下,如果您在终端中做过任何事情,您可能会看到~/.bash_history。
What It Means
You must create the file to set your default bash commands (commonly in ~/.bashrc). To do this, use any sort of editor, though it's more simple to do it within the terminal:
您必须创建文件来设置您的默认bash命令(通常在~/.bashrc中)。要做到这一点,请使用任何类型的编辑器,尽管在终端中执行它更简单:
- %> emacs .profile
- % > emacs . profile
- [from w/in emacs type:]
source ~/.bashrc
- [from w/in emacs type:] source ~/.bashrc。
- [from w/in emacs type:]
Ctrl + x Ctrl + s
(to save the file) - [from w/in emacs type:] Ctrl + x Ctrl + s(保存文件)
- [from w/in emacs type:]
Ctrl + x Ctrl + c
(to close emacs) - [从emacs类型:]Ctrl + x Ctrl + c(关闭emacs)
- %> emacs .bashrc
- % > emacs . bashrc
- [from w/in emacs type/paste all your bash commands, save, and exit]
- [从emacs类型/粘贴所有您的bash命令,保存和退出]
The next time you quit and reload the terminal, it should load all your bash preferences. For good measure, it's usually a good idea to separate your commands into useful file names. For instance, from within ~/.bashrc, you should have a source ~/.bash_aliases
and put all your alias commands in ~/.bash_aliases.
下一次当您退出并重新加载终端时,它应该加载您所有的bash首选项。为了更好地度量,将命令分隔成有用的文件名通常是个好主意。例如,从内部~/。bashrc,你应该有一个源~/。bash_aliases并将所有别名命令放入~/.bash_aliases中。
#7
18
What worked for me is to create a .launchd.conf
with the variables I needed:
为我工作的是创建一个。launchd。使用我需要的变量:
setenv FOO barbaz
This file is read by launchd at login. You can add a variable 'on the fly' to the running launchd using:
这个文件在登录时被launchd读取。您可以在运行的launchd中添加一个变量“on the fly”:
launchctl setenv FOO barbaz`
In fact, .launchd.cond
simply contains launchctl commands.
事实上,.launchd。cond只包含launchctl命令。
Variables set this way seem to be present in GUI applications properly.
这样设置的变量似乎在GUI应用程序中是正确的。
If you happen to be trying to set your LANG or LC_ variables in this way, and you happen to be using iTerm2, make sure you disable the 'Set locale variables automatically' setting under the Terminal tab of the Profile you're using. That seems to override launchd's environment variables, and in my case was setting a broken LC_CTYPE causing issues on remote servers (which got passed the variable).
如果您正试图以这种方式设置您的LANG或LC_变量,并且碰巧使用iTerm2,请确保在您使用的概要文件的终端选项卡下禁用“设置区域设置变量”。这似乎覆盖了launchd的环境变量,在我的例子中是在远程服务器上设置了一个损坏的LC_CTYPE,从而导致了问题(通过了变量)。
(The environment.plist still seems to work on my Lion though. You can use the RCenvironment preference pane to maintain the file instead of manually editing it or required Xcode. Still seems to work on Lion, though it's last update is from the Snow Leopard era. Makes it my personally preferred method.)
(环境。不过,plist似乎仍在我的狮子身上发挥作用。您可以使用RCenvironment配置窗格来维护文件,而不是手工编辑它或需要Xcode。尽管它的最新更新来自雪豹时代,但它似乎仍然对狮子有效。使它成为我个人喜欢的方法。
#8
14
Setup your PATH environment variable on Mac OS
Open the Terminal program (this is in your Applications/Utilites folder by default). Run the following command
打开终端程序(默认情况下,这是在您的应用程序/使用文件夹中)。运行以下命令
touch ~/.bash_profile; open ~/.bash_profile
This will open the file in the your default text editor.
这将在您的默认文本编辑器中打开该文件。
For ANDROID SDK as example :
以ANDROID SDK为例:
You need to add the path to your Android SDK platform-tools and tools directory. In my example I will use "/Development/android-sdk-macosx" as the directory the SDK is installed in. Add the following line:
您需要为Android SDK平台工具和工具目录添加路径。在我的例子中,我将使用“/开发/android-sdk-macosx”作为SDK安装的目录。添加以下行:
export PATH=${PATH}:/Development/android-sdk-macosx/platform-tools:/Development/android-sdk-macosx/tools
Save the file and quit the text editor. Execute your .bash_profile to update your PATH.
保存文件并退出文本编辑器。执行.bash_profile以更新路径。
source ~/.bash_profile
Now everytime you open the Terminal program you PATH will included the Android SDK.
现在,每次打开终端程序时,都会包含Android SDK。
#9
11
Adding Path Variables to OS X Lion
This was pretty straight forward and worked for me, in terminal:
这是非常直接的,对我来说,在终端:
$echo "export PATH=$PATH:/path/to/whatever" >> .bash_profile #replace "/path/to/whatever" with the location of what you want to add to your bash profile, i.e: $ echo "export PATH=$PATH:/usr/local/Cellar/nginx/1.0.12/sbin" >> .bash_profile
$. .bash_profile #restart your bash shell
A similar response was here: http://www.mac-forums.com/forums/os-x-operating-system/255324-problems-setting-path-variable-lion.html#post1317516
类似的响应也出现在这里:http://www.mac-forums.com/forums/os-x-operating-system/255324-问题- settingpath- variable-lion.html#post1317516。
#10
6
Open Terminal:
打开终端:
vi ~/.bash_profile
Apply changing to system (no need restart computer):
应用变更到系统(不需要重新启动计算机):
source ~/.bash_profile
(Also work with macOS Sierra 10.12.1)
(还与macOS Sierra 10.12.1合作)
#11
5
I had problem with Eclipse (started as GUI, not from script) on Maverics that it did not take custom PATH. I tried all the methods mentioned above to no avail. Finally I found the simplest working answer based on hints from here:
我遇到了Eclipse(从GUI开始,不是从脚本开始)的问题,因为它没有采用自定义路径。我尝试了上面提到的所有方法都无济于事。最后,我根据这里的提示找到了最简单的工作回答:
-
Go to /Applications/eclipse/Eclipse.app/Contents folder
去/应用程序/ eclipse / eclipse。应用程序/内容文件夹
-
Edit Info.plist file with text editor (or XCode), add LSEnvironment dictionary for environment variable with full path. Note that it includes also /usr/bin etc:
编辑信息。使用文本编辑器(或XCode)的plist文件,为环境变量添加完整路径的LSEnvironment字典。注意,它还包括/usr/bin等:
<dict> <key>LSEnvironment</key> <dict> <key>PATH</key> <string>/usr/bin:/bin:/usr/sbin:/sbin:/dev/android-ndk-r9b</string> </dict> <key>CFBundleDisplayName</key> <string>Eclipse</string> ...
-
Reload parameters for app with
应用程序的重载参数。
/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -v -f /Applications/eclipse/Eclipse.app
-
Restart Eclipse
重新启动Eclipse
#12
4
I took the idiot route. Added these to the end of /etc/profile
我选择了白痴路线。将这些添加到/etc/profile的末尾。
for environment in `find /etc/environments.d -type f`
do
. $environment
done
created a folder /etc/environments create a file in it called "oracle" or "whatever" and added the stuff I needed set globally to it.
创建一个文件夹/etc/environment创建一个名为“oracle”或“whatever”的文件,并添加我需要在全局上设置的内容。
/etc$ cat /etc/environments.d/Oracle
猫/etc/environments.d美元/ etc / Oracle
export PATH=$PATH:/Library/Oracle/instantclient_11_2
export DYLD_LIBRARY_PATH=/Library/Oracle/instantclient_11_2
export SQLPATH=/Library/Oracle/instantclient_11_2
export PATH=$PATH:/Library/Oracle/instantclient_11_2
export TNS_ADMIN=/Library/Oracle/instantclient_11_2/network/admin
#13
3
Let me illustrate you from my personal example in a very redundant way.
让我用一种非常多余的方式来说明我个人的例子。
- First after installing JDK, make sure it's installed.
- 在安装JDK之后,首先要确保安装了JDK。
-
Sometimes macOS or Linux automatically sets up environment variable for you unlike Windows. But that's not the case always. So let's check it. The line immediately after echo $JAVA_HOME would be empty if the environment variable is not set. It must be empty in your case.
有时macOS或Linux会自动为您设置环境变量,而不是Windows。但事实并非如此。让我们检查一下。如果环境变量没有设置,那么echo $JAVA_HOME后面的行将是空的。在您的情况下,它必须是空的。
-
Now we need to check if we have bash_profile file. You saw that in my case we already have bash_profile. If not we have to create a bash_profile file.
现在我们需要检查是否有bash_profile文件。在我的例子中,我们已经有bash_profile。如果不是,我们必须创建bash_profile文件。
-
创建一个bash_profile文件。
-
Check again to make sure bash_profile file is there.
再次检查以确保bash_profile文件在那里。
-
Now let's open bash_profile file. macOS opens it using it's default TextEdit program.
现在让我们打开bash_profile文件。macOS使用它的默认TextEdit程序打开它。
-
This is the file where environment variables are kept. If you have opened a new bash_profile file, it must be empty. In my case, it was already set for python programming language and Anaconda distribution. Now, i need to add environment variable for Java which is just adding the first line. YOU MUST TYPE the first line VERBATIM. JUST the first line. Save and close the TextEdit. Then close the terminal.
这是保存环境变量的文件。如果您已经打开了一个新的bash_profile文件,那么它必须是空的。在我的例子中,它已经设置为python编程语言和Anaconda分布。现在,我需要为Java添加环境变量,它只是添加了第一行。你必须逐字输入第一行。第一行。保存并关闭TextEdit。然后关闭终端。
-
Open the terminal again. Let's check if the environment variable is set up.
再次打开终端。让我们检查一下环境变量是否设置好了。
#14
1
More detail, which may perhaps be helpful to someone:
更多的细节,也许对某人有帮助:
Due to my own explorations, I now know how to set environment variables in 7 of 8 different ways. I was trying to get an envar through to an application I'm developing under Xcode. I set "tracer" envars using these different methods to tell me which ones get it into the scope of my application. From the below, you can see that editing the "scheme" in Xcode to add arguments works, as does "putenv". What didn't set it in that scope: ~/.MACOS/environment.plist, app-specific plist, .profile, and adding a build phase to run a custom script (I found another way in Xcode [at least] to set one but forgot what I called the tracer and can't find it now; maybe it's on another machine....)
由于我自己的探索,我现在知道如何用8种不同的方式来设置环境变量。我试图将envar应用到Xcode中开发的应用程序中。我用这些不同的方法设置了“tracer”envars,告诉我哪些方法可以进入我的应用程序的范围。从下面,您可以看到在Xcode中编辑“scheme”来添加参数,就像“putenv”一样。在这个范围内没有设置的是:~/. macos /环境。plist,应用程序特定的plist, .profile,并添加一个构建阶段来运行一个自定义脚本(我在Xcode中找到了另一种方法[至少]设置一个,但是忘记了我所称的跟踪器,现在找不到它;可能是在另一台机器上
GPU_DUMP_DEVICE_KERNEL is 3
GPU_DUMP_DEVICE_KERNEL是3
GPU_DUMP_TRK_ENVPLIST is (null)
GPU_DUMP_TRK_ENVPLIST(空)
GPU_DUMP_TRK_APPPLIST is (null)
GPU_DUMP_TRK_APPPLIST(空)
GPU_DUMP_TRK_DOTPROFILE is (null)
GPU_DUMP_TRK_DOTPROFILE(空)
GPU_DUMP_TRK_RUNSCRIPT is (null)
GPU_DUMP_TRK_RUNSCRIPT(空)
GPU_DUMP_TRK_SCHARGS is 1
GPU_DUMP_TRK_SCHARGS是1
GPU_DUMP_TRK_PUTENV is 1
GPU_DUMP_TRK_PUTENV是1
... on the other hand, if I go into Terminal and say "set", it seems the only one it gets is the one from .profile (I would have thought it would pick up environment.plist also, and I'm sure once I did see a second tracer envar in Terminal, so something's probably gone wonky since then. Long day....)
…另一方面,如果我进入终端并说“set”,它似乎是唯一一个从。profile(我本以为它会选择环境)的。plist也是,我确定一旦我在终端看到了第二个tracer envar,那么从那以后可能就会变得不稳定了。漫长的一天....)
#15
-3
Step1: open ~/.bash_profile
步骤1:打开~ / . bash_profile
Now a text editor opens:
现在,一个文本编辑器打开:
Step2: variable name should be in capitals. in this example variable is NODE_ENV
步骤2:变量名应该大写。在这个例子中变量是NODE_ENV。
Step3: export NODE_ENV=development
步骤3:出口NODE_ENV =发展
Save it and close.
保存并关闭。
Restart your system.
重新启动您的系统。
Done.
完成了。
To check env variable: open terminal and type
检查env变量:打开终端和类型。
echo $NODE_ENV