Course contentsShow
← Linux
Lesson 12 of 1,8191. Shell Architecture and Process EnvironmentFree lesson

Shell Startup and Initialization Files

Differentiate between login and non-login shells to control environment initialization patterns.

In this lesson, you will learn how the shell decides which configuration files to read when it starts up by distinguishing between "login" and "non-login" shells.

When you open your terminal, the shell needs to set up your environment—like defining your prompt or custom aliases. However, it doesn't process the same files every time you run a command. It behaves differently depending on how the session was initiated.

Login Shells

A login shell is the first shell you interact with after you successfully log into your system (like when you SSH into a server or log in via a physical terminal). Its primary job is to establish your complete user environment. Because this is the "start" of your session, it looks for initialization files that set global variables and paths. For bash, this typically involves reading /etc/profile and then looking for ~/.bash_profile or ~/.profile in your home directory.

Non-Login Shells

A non-login shell is any shell you start after you are already logged in. For example, if you are already in a terminal and you type bash to start a new sub-shell, or if you open a new terminal tab in a GUI environment, you are triggering a non-login shell.

Because you are already "inside" the session, these shells assume your global environment is already set up. Instead of looking for login-specific files, they focus on interactive settings. For bash, these shells skip the login files and read ~/.bashrc instead.

Why the distinction matters

Consider this scenario: You add a command to ~/.bash_profile to print "Hello!" every time you log in. If you open a new tab in your terminal emulator, you won't see that greeting. That is because the terminal tab is a non-login shell, so it ignores your .bash_profile entirely.

To test which shell you are currently using, you can run this command:

echo $0

If the output starts with a hyphen (like -bash), it is a login shell. If it is just the name of the shell (like bash), it is a non-login shell.

Key Takeaway: Login shells initialize your full environment upon entry, while non-login shells assume the environment is inherited and load only session-specific configurations.