1.2 Special Files
1. Special Files
Files (like .profile, .bash_profile, .bashrc, etc.) are called “special files” because they have specific, predefined purposes within the shell environment that control and customize the behavior of the shell session.
1.1 What is .profile
?
The .profile
file is a shell script that is executed when a login shell starts. It is used to set up user-specific environment variables, shell configurations, and other initialization tasks. It is typically found in the user’s home directory (/home/username/.profile
), and it is executed for interactive login shells (such as when you log in via a terminal or SSH).
- Purpose: The
.profile
file is used to set environment variables, change the default shell behavior, and run commands that should be initialized at login (like setting thePATH
, customizing the prompt, or running background processes). - When It Runs: It runs when you log into the system (for example, via SSH or using a terminal that starts a login shell).
1.2 What is Sourcing?
“Sourcing” refers to the act of executing a script within the current shell session rather than in a new shell. This is done using the source
command (or .
which is shorthand) to execute the contents of a file in the current shell environment.
-
Why Source? When you source a script, the changes made to the environment (like variables and functions) persist in the current session. This is in contrast to executing a script normally, which runs in a subshell and does not affect the current shell environment.
-
Syntax:
Terminal window source filenameor
Terminal window . filenameFor example, if you have a script
my_config.sh
that sets some environment variables, you can source it like this:Terminal window source my_config.sh
This would execute my_config.sh
in the current shell, and any variables or functions defined in that script would persist in the current session.
1.3 Other Related or Similar Files
In addition to .profile
, there are several other files that can be used to configure your shell environment, especially in bash and other similar shells:
-
.bash_profile
:- Purpose: Like
.profile
, the.bash_profile
file is executed when you start an interactive login shell. It is specific to the Bash shell, and often contains environment variables and configurations that apply only to that shell. - Relation to
.profile
: On many systems,.bash_profile
is used instead of.profile
, and.bash_profile
will often source.profile
to maintain compatibility. - When It Runs: It runs for interactive login shells (e.g., when logging in via SSH or opening a terminal session in certain configurations).
Terminal window # Commonly found in ~/.bash_profileif [ -f ~/.profile ]; thensource ~/.profilefi - Purpose: Like
-
.bashrc
:- Purpose: The
.bashrc
file is used for interactive non-login shells. This means that it is executed when you open a new terminal session (e.g., runninggnome-terminal
or starting a new terminal window from the desktop). - When It Runs: It runs for interactive non-login shells (e.g., when opening a new terminal window).
- Common Usage: It’s used for setting shell options, functions, aliases, and prompt customization that you want available in all new terminal windows.
Terminal window # Commonly found in ~/.bashrcexport PATH=$PATH:/some/new/pathalias ll='ls -la' - Purpose: The
-
.profile
vs.bash_profile
:.profile
: This is a generic initialization file that is used by many shells, not just Bash. It’s used for login shells..bash_profile
: This is specific to Bash and is used for login shells. It’s common to see.bash_profile
sourcing.profile
to ensure compatibility with non-Bash login shells.
-
.bash_logout
:- Purpose: This file is executed when you log out of a Bash login shell. It can be used to execute commands that you want to run when logging out, like clearing temporary files or running custom scripts.
- When It Runs: It runs when you log out of an interactive Bash login shell.
Terminal window # Commonly found in ~/.bash_logoutecho "Goodbye, see you next time!" -
.zshrc
,.zprofile
,.zlogin
, etc. (for Zsh users):- Zsh has similar initialization files, such as
.zshrc
,.zprofile
,.zlogin
, and.zlogout
, which serve similar purposes as the corresponding Bash files. .zshrc
: Equivalent to.bashrc
, used for interactive non-login shells..zprofile
: Equivalent to.bash_profile
, used for login shells..zlogin
: Similar to.bash_login
, it runs for login shells but after.zprofile
..zlogout
: Runs when you log out from a Zsh session.
- Zsh has similar initialization files, such as
1.4 Key Differences
File | Shell Type | Runs On Login | Runs On Non-login | Typical Purpose |
---|---|---|---|---|
.profile | Any POSIX shell | Yes | No | Set user-specific environment variables, etc. |
.bash_profile | Bash | Yes | No | Used for Bash login shells (often sources .profile ) |
.bashrc | Bash | No | Yes | Used for interactive non-login Bash shells (customize prompts, aliases, etc.) |
.bash_logout | Bash | No | No | Runs when logging out from a Bash session |
.zshrc | Zsh | No | Yes | Used for interactive non-login Zsh shells |
.zprofile | Zsh | Yes | No | Used for login Zsh shells |
.zlogin | Zsh | Yes | No | Runs after .zprofile for Zsh login shells |
.zlogout | Zsh | No | No | Runs when logging out from a Zsh session |
2. Source Files
Sourcing a file means executing the script’s commands in the current shell environment. Here’s how you can source the various initialization files:
-
Source
.bashrc
in.bash_profile
: Since.bashrc
is typically used for non-login shells (like opening a terminal), it’s common to source.bashrc
from.bash_profile
to ensure the same environment variables and settings are available in login shells.Terminal window # In .bash_profileif [ -f ~/.bashrc ]; thensource ~/.bashrcfi -
Source a File Manually: You can also source any shell script file manually by running:
Terminal window source filenameor simply:
Terminal window . filenameThis will execute the commands in the specified file within the current shell session.