What is the proper way to modify environment variables like PATH in OS X?
I\'ve looked on Google a little bit and found three different files to edit:
I think the OP is looking for a simple, Windows-like solution.
Here you go:
http://www.apple.com/downloads/macosx/system_disk_utilities/environmentvariablepreferencepane.html
Well, I'm unsure about the /etc/paths
and ~/.MacOSX/environment.plist
files. Those are new.
But with Bash, you should know that .bashrc
is executed with every new shell invocation
and .bash_profile
is only executed once at startup.
I don't know how often this is with Mac OS X. I think the distinction has broken down with the window system launching everything.
Personally, I eliminate the confusion by creating a .bashrc
file with everything I need and then do:
ln -s .bashrc .bash_profile
For Bash, try adding your environment variables to the file /etc/profile
to make them available for all users. No need to reboot, just start a new Terminal session.
There are essentially two problems to solve when dealing with
environment variables in OS X. The first is when invoking programs
from Spotlight (the magnifying glass icon on the right side of the Mac
menu/status bar) and the second when invoking programs from the Dock.
Invoking programs from a Terminal application/utility is trivial
because it reads the environment from the standard shell locations
(~/.profile
, ~/.bash_profile
, ~/.bashrc
, etc.)
When invoking programs from the Dock, use ~/.MacOSX/environment.plist
where the <dict>
element contains a sequence of
<key>KEY</key><string>theValue</string>
elements.
When invoking programs from Spotlight, ensure that launchd has been setup with all the key/value settings you require.
To solve both problems simultaneously, I use a login item (set via the
System Preferences tool) on my User account. The login item is a bash script that
invokes an Emacs lisp function although one can of course use their
favorite scripting tool to accomplish the same thing. This approach
has the added benefit that it works at any time and does not require a
reboot, i.e. one can edit ~/.profile
, run the login item in some shell and have the changes visible for newly invoked programs, from either the Dock or Spotlight.
Details:
Login item: ~/bin/macosx-startup
#!/bin/bash
bash -l -c "/Applications/Emacs.app/Contents/MacOS/Emacs --batch -l ~/lib/emacs/elisp/macosx/environment-support.el -f generate-environment"
Emacs lisp function: ~/lib/emacs/elisp/macosx/envionment-support.el
;;; Provide support for the environment on Mac OS X
(defun generate-environment ()
"Dump the current environment into the ~/.MacOSX/environment.plist file."
;; The system environment is found in the global variable:
;; 'initial-environment' as a list of "KEY=VALUE" pairs.
(let ((list initial-environment)
pair start command key value)
;; clear out the current environment settings
(find-file "~/.MacOSX/environment.plist")
(goto-char (point-min))
(setq start (search-forward "<dict>\n"))
(search-forward "</dict>")
(beginning-of-line)
(delete-region start (point))
(while list
(setq pair (split-string (car list) "=")
list (cdr list))
(setq key (nth 0 pair)
value (nth 1 pair))
(insert " <key>" key "</key>\n")
(insert " <string>" value "</string>\n")
;; Enable this variable in launchd
(setq command (format "launchctl setenv %s \"%s\"" key value))
(shell-command command))
;; Save the buffer.
(save-buffer)))
NOTE: This solution is an amalgam of those coming before I added mine, particularly that offered by Matt Curtis, but I have deliberately tried to keep my ~/.bash_profile
content platform independent and put the setting of the launchd
environment (a Mac only facility) into a separate script.
All the magic on iOS only goes with using source
with the file, where you export your environment variables.
For example:
You can create an file like this:
export bim=fooo
export bom=bar
Save this file as bimbom.env
, and do source ./bimbom.ev
.
Voilá, you got your environment variables.
Check them with:
echo $bim
There are two type of shells at play here.
It's important to understand here that with Bash, file .bashrc
is only read by a shell that's both interactive and non-login, and you will find that people often load .bashrc
in .bash_profile
to overcome this limitation.
Now that you have the basic understanding, let’s move on to how I would advice you to set it up.
.bash_file:
#!/bin/bash
source ~/.profile # Get the PATH settings
source ~/.bashrc # Get Aliases and Functions
#