Mac Os X Set Environment Variable For Application
Creating a file for variable name and paste the path there under /etc/paths.d and source the file to profilebashrc. Export path variable in /.profilebashrc as. Export VARIABLENAME = $(PATHVALUE) AND source the the path. Its simple and stable. You can set any path variable by Mac terminal or in linux also.
launchd.plist is not a file, but rather a type of file. It is an XML document that holds parameters which can be loaded into launchd
. Here we’ll see how to use it to set environment variables.
Setting a Variable
For our first example, we’ll set the variable FOO
to the value of BAR
. Let’s look at an example and we’ll break it down:
Breakdown
We will save in /Library/LaunchDaemons
as that is where system-wide daemons provided by the administrator are set up. Other options include:
Not supported in Mac OS X 10.10 and later. With launchd.plist, the same things can be accomplished as with /etc/launchd.conf but formatted in an XML document. Parts of this series. HowTo: Set an Environment Variable in Mac OS X; HowTo: Set an Environment Variable in Mac OS X - Prerequisites; HowTo: Set an Environment Variable in Mac OS X. HowTo: Set an Environment Variable in Mac OS X - Terminal Only The process of setting environment variables in the Terminal will be familiar to those with experience with Linux or UNIX. It involves editing files in /etc for global environment variables, and in your home directory for. Alternatively, you can also add the following command to your.bashprofileif you want your environment variables to be visible by graphic applications. In Mac OS X, graphic applications do not inherit your.bashprofile config: launchctl setenv MYPATH myvar. This tutorial will show you how to set your environment variables for use by both terminal and GUI applications in Mac OS X.
/Library/LaunchAgents
- which is where per-user agents provided by the administrator are setup.~/Library/LaunchAgents
- which is where per-user agents provided by the user are setup.
We’ll name the file setenv.FOO.plist
, as the convention is to name the file Label.plist
and the label here is setenv.FOO
.
Label
Mac Set Environment Variable Terminal
The label can be anything, but needs to uniquely identify the job. We’ll use a convention of setenv.VARIABLE_NAME
.
ProgramArguments
Next is the program arguments of the daemon. launchctl
is used to set environment variables; it can be used in the terminal to set environment variables. This is what it would look like using the terminal:
Uninstall Mac Os X Applications
Quit the terminal and reopen it
The issue however is if you reboot, you’ll lose the environment variable; fortunately we can put this command into ProgramArguments
. ProgramArguments
is just an array that lists the arguments for running the program.
RunAtLoad
This tells launchd
whether or not to run the job once it is read.
ServiceIPC
This was required in Mac OS X 10.4, but is inferred in Mac OS X 10.5 and later.
Creating the File
We can run the following command in the terminal to create the file.
Load the file
And if we don’t want to log out and back in, we can just run this command in the terminal to set the variable.
Setting Another Variable
When setting variables with /etc/launchd.conf
we couldn’t have spaces in the value, here we can. We’ll set the environment variable BAR
to the value TEST SPACES
:
Create the File
Load the File
Issues
launchd
does not have ordering for loading services, so setting the environment variables will be run concurrently with any programs automatically opening on login. So can either close and reopen those apps that need the variables; login, then logout, then login again; or do not use “Reopen windows when logging back”.
References
Parts of this series
- HowTo: Set an Environment Variable in Mac OS X - launchd.plist
Here we will talk about some things you’ll need for setting environment variables in Mac OS X.
Terminal
You will find Terminal in the Utilities folder in the Applications folder.
Print environment variables
Print all environment variables:
Print a specific environment variable:
Text Editors
Depending on which method you pick, you’ll need to edit some text files
GUI
TextWrangler
If you pick TextWrangler, we’ll be opening files with File
> Open File by Name...
- TextWrangler - Download
Xcode
If you pick Xcode, we’ll be opening files with File
> Open Quickly...
.
If you use the ~/.MacOSX/environment.plist
approach, you’ll need Xcode for editing plist
files.
- for Mac OS X 10.7 - Xcode 4 on the Mac App Store
- for Mac OS X 10.6 - Xcode 3.2.6 and iOS SDK 4.3
In Terminal
The following editors are already installed.
vi
/vim
pico
/nano
emacs
You will need to use sudo
for open some files, notably in /etc
.
Example:
Set Environment Variables Windows 10
Parts of this series
- HowTo: Set an Environment Variable in Mac OS X - Prerequisites