Introduction
Shell profile files are scripts that configure your environment when a shell session starts. Understanding their execution order and purpose is essential for anyone working with UNIX/Linux systems.
Shell Types
- Login Shell: First shell after login (SSH, console, or
bash --login). - Non-login Interactive Shell: Terminal opened from GUI or
bashwithout--login. - Non-interactive Shell: Scripts or commands executed without user interaction.
Profile Files Overview
Common profile files include:
/etc/profile– System-wide settings for login shells.~/.profile– User-specific settings for POSIX shells.~/.bash_profile– Bash login shell settings.~/.bashrc– Bash interactive non-login shell settings.
Execution Precedence
The order in which files are read depends on the shell type:
| Shell Type | Files Read |
|---|---|
| Login (bash) | /etc/profile, then ~/.bash_profile or ~/.profile |
| Non-login (bash) | ~/.bashrc |
| POSIX sh | /etc/profile, ~/.profile |
Best Practices
- Do not mix shell-specific syntax in generic files like
~/.profile. - Use
~/.bashrcfor aliases and interactive settings. - Source
~/.bashrcfrom~/.bash_profile:if [ -f ~/.bashrc ]; then . ~/.bashrc fi
Forcing a Specific Shell
To change your login shell (requires root):
sudo chsh -s /bin/bash username
Without root, add this to ~/.profile:
if [ -x /bin/bash ]; then
exec /bin/bash
fi
Key Things to Know
- Export environment variables in profile files for persistence.
- Append to PATH instead of overwriting it.
- Do not add writable directories to PATH for security.
- Bash supports advanced features like
PROMPT_COMMANDand process substitution.