HowTo: Set an Environment Variable in Mac OS X - Terminal Only
(from : http://www.dowdandassociates.com/blog/content/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
Global
If you just want to edit the global
Open a new Terminal and
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 user specific environment variables.Login shell vs non-login shell
A login shell is the shell that is started when you open a new terminal window. A non-login shell is when you start a sub-shell in the window, for example, typingbash
when you have the bash shell running already.
Global PATH
only
If you just want to edit the global PATH
variable, you can put a file in /etc/paths.d
with a the path you want to add. For example, we will create a file /etc/paths.d/ec2-api-tools
and put the following in:/etc/paths.d/ec2-api-tools
| /opt/aws/ec2-api-tools/bin
|
$PATH
will reflect the change.Bash
Setting environment variables
The format for setting an environment variable in the bash shell isKEY=value
with no spaces around the equal sign. In addition, when putting it in a configuration file it is necessary to prepend it with export
.
The following is an example of how to set an environment variable in
one of the configuration files, it also works on the command line; we
will set the variable JAVA_HOME
to /Library/Java/Home
:Terminal - hostname:~ user$
| export JAVA_HOME=/Library/Java/Home
|
Login shell
First/etc/profile
, then whichever of the following exists, trying them in order: ~/.bash_profile
, ~/.bash_login
, or ~/.profile
Non-login shell
/etc/bashrc
, then ~/.bashrc
Have login shell read /etc/paths and /etc/paths.d
/etc/profile (Excerpt)
| if [ -x /usr/libexec/path_helper ]; then
eval `/usr/libexec/path_helper -s`
fi
|
Have login shell read /etc/bashrc and ~/.bashrc
/etc/profile (Excerpt)
| if [ "${BASH-no}" != "no" ]; then
[ -r /etc/bashrc ] && . /etc/bashrc
fi
|
~/.bash_profile (Excerpt)
| [[ -f ~/.bashrc ]] && . ~/.bashrc
|
Setup /etc/profile.d
/etc/profile (Excerpt)
| for sh in /etc/profile.d/*.sh ; do
[ -r "$sh" ] && . "$sh"
done
unset sh
|
Terminal - hostname:~ user$
| sudo mkdir /etc/profile.d
|
Resources
The following is the collection of all the changes above along with links to download them:/etc/profile
| # System-wide .profile for sh(1)
if [ -x /usr/libexec/path_helper ]; then
eval `/usr/libexec/path_helper -s`
fi
if [ "${BASH-no}" != "no" ]; then
[ -r /etc/bashrc ] && . /etc/bashrc
fi
for sh in /etc/profile.d/*.sh ; do
[ -r "$sh" ] && . "$sh"
done
unset sh
|
/etc/bashrc
| # System-wide .bashrc file for interactive bash(1) shells.
if [ -z "$PS1" ]; then
return
fi
PS1='\h:\W \u\$ '
# Make bash check its window size after a process completes
shopt -s checkwinsize
# Tell the terminal about the working directory at each prompt.
if [ "$TERM_PROGRAM" == "Apple_Terminal" ] && [ -z "$INSIDE_EMACS" ]; then
update_terminal_cwd() {
# Identify the directory using a "file:" scheme URL,
# including the host name to disambiguate local vs.
# remote connections. Percent-escape spaces.
local SEARCH=' '
local REPLACE='%20'
local PWD_URL="file://$HOSTNAME${PWD//$SEARCH/$REPLACE}"
printf '\e]7;%s\a' "$PWD_URL"
}
PROMPT_COMMAND="update_terminal_cwd; $PROMPT_COMMAND"
fi
|
~/.bash_profile
| # ~/.bash_profile
# This file is sourced by bash for login shells. The following line
# runs your .bashrc and is recommended by the bash info pages.
[[ -f ~/.bashrc ]] && . ~/.bashrc
|
~/.bashrc
| # ~/.bashrc
#
# This file is sourced by all *interactive* bash shells on startup,
# including some apparently interactive shells such as scp and rcp
# that can't tolerate any output. So make sure this doesn't display
# anything or bad things will happen !
# Test for an interactive shell. There is no need to set anything
# past this point for scp and rcp, and it's important to refrain from
# outputting anything in those cases.
if [[ $- != *i* ]] ; then
# Shell is non-interactive. Be done now!
return
fi
# Put your fun stuff here.
|
Tcsh
Setting environment variables
For the tcsh shell, the format for setting an environment variable issetenv KEY value
. The following is an example which will set the JAVA_HOME
variable to /Library/Java/Home
.Terminal - [hostname:~] %
| setenv JAVA_HOME /Library/Java/Home
|
Login shell
First/etc/csh.cshrc
, then /etc/csh.login
, then whichever of the following exists, trying in order: ~/.tcshrc
, ~/.cshrc
; then finally ~/.login
.Non-login shell
First/etc/csh.cshrc
, then whichever of the following exists, trying in order: ~/.tcshrc
, ~/.cshrc
.References
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 - Terminal Only
- HowTo: Set an Environment Variable in Mac OS X - $HOME/.MacOSX/environment.plist
- HowTo: Set an Environment Variable in Mac OS X - /etc/launchd.conf
- HowTo: Set an Environment Variable in Mac OS X - launchd.plist