03. Bash startup files .bash_profile, .bashrc

When the bash starts up, it reads configuration files to set up the environment. These files are known as startup files.

Startup files work by first loading the default environment shared by all users. Then it loads files in the user's home directory to give a more tailored experience per user.

Two types of shell logins

There are two types of shell logins: login and non-login shell sessions. Depending on which one you choose to boot your shell, a different group of startup files are executed.

Login shell sessions

For a login shell session, a username and password in necessary.

Here is the order in which the startup files are executed in a login shell.

/etc/profile
Global configuration file that loads for all users.
Automatically read into shell if login shell.
/etc/bashrc
Global configuration file that loads for all users.
~/.bash_profile
The user's personal startup file.
Oftentimes the .bash_profile file contains code that executes the .bashrc file.
~/.bash_login
This file is executed if a user's .bash_profile is missing.
~/.profile
A fallback file if the above two are not found.
~/.bash_logout
Executed when a user logs out.

Non-login shell sessions

Non-login shells inherit the environment from their parent process.

/etc/bash.bashrc
Configuration file for all users.
~/.bashrc
Startup file.

The main difference in files being executed is that login shells run .bash_profile on startup, while non-login shells run .bashrc. However, .bashrc is usually executed from .bash_profile.

Hidden files

Remember that the . before a file name means that it is hidden. To view hidden files, use ls -a.

Configuring our environment

There is a convention of where to store certain variables when configuring your environment.

If you want to add any directories to your $PATH, you should do it in your ~/.bash_profile. For all others (configuring aliases or setting umask), place it in ~/.bashrc.

In the next section, let's talk about how to properly set environment variables so that your settings are loaded upon every login.

Take your Linux skills to the next level!

The Linux Command Line

Take your Linux skills to the next level! Try Linux & UNIX

The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by generations of gray-bearded, mouse-shunning gurus: file navigation, environment configuration, command chaining, pattern matching with regular expressions, and more.

$ Check price
39.9539.95Amazon 4.5 logo(274+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Acupressure Mat & Pillow

Aching back from coding all day? Try Back Problems

Relieve your stress, back, neck and sciatic pain through 1,782 acupuncture points for immediate neck pain relief. Made for lower, upper and mid chronic back pain treatment, and improves circulation, sleep, digestion and quality of life.

$$ Check price
144.87144.87Amazon 4.5 logo(1,890+ reviews)

More Back Problems resources

Ad