Skip to content

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 the PATH, 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 filename

    or

    Terminal window
    . filename

    For 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.

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:

  1. .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_profile
    if [ -f ~/.profile ]; then
    source ~/.profile
    fi
  2. .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., running gnome-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 ~/.bashrc
    export PATH=$PATH:/some/new/path
    alias ll='ls -la'
  3. .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.
  4. .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_logout
    echo "Goodbye, see you next time!"
  5. .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.

1.4 Key Differences

FileShell TypeRuns On LoginRuns On Non-loginTypical Purpose
.profileAny POSIX shellYesNoSet user-specific environment variables, etc.
.bash_profileBashYesNoUsed for Bash login shells (often sources .profile)
.bashrcBashNoYesUsed for interactive non-login Bash shells (customize prompts, aliases, etc.)
.bash_logoutBashNoNoRuns when logging out from a Bash session
.zshrcZshNoYesUsed for interactive non-login Zsh shells
.zprofileZshYesNoUsed for login Zsh shells
.zloginZshYesNoRuns after .zprofile for Zsh login shells
.zlogoutZshNoNoRuns 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_profile
    if [ -f ~/.bashrc ]; then
    source ~/.bashrc
    fi
  • Source a File Manually: You can also source any shell script file manually by running:

    Terminal window
    source filename

    or simply:

    Terminal window
    . filename

    This will execute the commands in the specified file within the current shell session.