profile.d osx
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 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, typing
bash
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 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
1
/opt/aws/ec2-api-tools/bin
Open a new Terminal and
$PATH
will reflect the change.Bash
Setting environment variables
The format for setting an environment variable in the bash shell is
KEY=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$
1
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)
1
2
3
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)
~/.bash_profile (Excerpt)
1
[[ -f ~/.bashrc ]] && . ~/.bashrc
Setup /etc/profile.d
/etc/profile (Excerpt)
1
2
3
4
for sh in /etc/profile.d/*.sh ; do
[ -r "$sh" ] && . "$sh"
done
unset sh
Terminal - hostname:~ user$
1
sudo mkdir /etc/profile.d
Resources
The following is the collection of all the changes above along with links to download them:
/etc/profile
/etc/bashrc
~/.bash_profile
1
2
3
4
5
# ~/.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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# ~/.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 is
setenv KEY value
. The following is an example which will set the JAVA_HOME
variable to /Library/Java/Home
.Terminal - [hostname:~] %
1
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
.
Nenhum comentário:
Postar um comentário