OSXFAQ Mac OS X UNIX Tip-of-the-Day

Week 105 - Controlling Bash At Startup (18 April 2005)
by
Adrian Mayo - Editor, OSXFAQ
Wednesday - Initialisation Scripts
The four different initialisation scripts (see Monday's tip) must be set up correctly to ensure shells have the correct environment.
First off, /etc/profile and /etc/bashrc are system-wide. Use these scripts to set up the environment that is common to all users. ~/.bash_profile and ~/.bashrc are specific to each user and are used to add personal settings to the environment.
Second, /etc/profile and ~/.bash_profile are sourced by login shells and therefore applied once for each terminal session. Typing 'bash' at the command line runs a non-login shell which sources /etc/bashrc and ~/.bashrc.
A good strategy is to do the following:
Use /etc/profile and ~/.bash_profile to set environment variables. Environment variables are inherited by non-login shells and don't need to be setup again.
Use /etc/bashrc and ~/.bashrc to set shell variables and other shell settings. Shell variables are not inherited by non-login shells and therefore need to be set each time.
Finally, /etc/profile should source /etc/bashrc:
source /etc/bashrc
and ~/.bash_profile should source ~/.bashrc:
source ~/.bashrc
to avoid having to set shell variables in both the login and non-login scripts. Note that these must be 'sourced' and not simply called like:
/etc/bashrc
If you want to learn more about Mac OS X Unix visit the Learning Center
click.
- For beginners: Mac OS X Unix Tutorials
- For detailed information on specific topics: Advanced Unix
- For OS X in gereral: Mac OS X Tutorials
|