What is the difference between a login shell and a non-login shell in Linux?
Learn the difference between login and non-login shells in Linux, which startup files each reads, and where to put PATH, variables, and aliases correctly.
Expected Interview Answer
A login shell is the first shell started when you authenticate (via console, SSH, or su -), and it reads login profile files like /etc/profile and ~/.bash_profile; a non-login shell is any shell started afterward without authentication, and it reads ~/.bashrc instead.
The distinction controls which startup files run and therefore what environment variables and settings are available. A login bash shell sources /etc/profile then the first found of ~/.bash_profile, ~/.bash_login, or ~/.profile. An interactive non-login shell (like opening a new terminal tab) sources ~/.bashrc. Because of this, environment variables meant to be inherited by all processes are placed in profile files, while aliases and shell functions go in ~/.bashrc, which login profiles usually source explicitly.
- Predictable environment setup across sessions
- Correct placement of exported variables vs aliases
- Consistent behavior between SSH, console, and terminal tabs
- Easier debugging of 'command not found' or missing PATH issues
- Avoids duplicated or missing configuration
AI Mentor Explanation
A login shell is like the pre-match team meeting where the coach sets the game plan, kit, and batting order that apply all day. A non-login shell is a between-overs huddle that reuses that already-decided plan and only tweaks small tactics, never re-reading the full briefing from scratch.
Step-by-Step Explanation
Step 1
Identify how the shell started
Check whether authentication happened: console login, SSH, or su - create login shells; new terminal tabs create non-login shells.
Step 2
Know the login startup order
Bash login shells read /etc/profile then the first of ~/.bash_profile, ~/.bash_login, or ~/.profile.
Step 3
Know the non-login startup file
Interactive non-login bash shells read ~/.bashrc (often after /etc/bash.bashrc).
Step 4
Place configuration correctly
Put exported environment variables in profile files and aliases/functions in ~/.bashrc; have the profile source ~/.bashrc so both are covered.
Step 5
Verify the shell type
Run 'shopt -q login_shell && echo login || echo non-login' or check with echo $0 (a leading dash means login).
What Interviewer Expects
- Knowing which files each shell type sources
- Understanding the login startup file precedence
- Where to place exported variables vs aliases
- How SSH, su, and terminal tabs differ
- A way to detect the current shell type
Common Mistakes
- Assuming ~/.bashrc always runs for login shells
- Putting exported PATH changes only in ~/.bashrc
- Confusing interactive vs login as the same thing
- Forgetting that /etc/profile runs before user files
- Not sourcing ~/.bashrc from the login profile
Best Answer (HR Friendly)
“A login shell is the one you get when you actually log in, like over SSH, and it reads your main profile settings. A non-login shell is one you open afterward, like a new terminal tab, and it reads a lighter config file, so where you put settings determines whether they show up.”
Code Example
# Check if the current shell is a login shell
shopt -q login_shell && echo "login shell" || echo "non-login shell"
# echo $0 shows a leading dash for login shells: -bash
echo "$0"
# Common pattern in ~/.bash_profile so login shells also get ~/.bashrc
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# Force a login shell explicitly
bash --login
# Start a non-login interactive shell
bashFollow-up Questions
- Which files does a login bash shell read and in what order?
- What is the difference between interactive and non-interactive shells?
- Why might a PATH set in ~/.bashrc not appear over SSH?
- What does su versus su - change about the shell environment?
- How do zsh startup files differ from bash's?
MCQ Practice
1. Which file does an interactive non-login bash shell read by default?
Interactive non-login bash shells source ~/.bashrc, while login shells use the profile files.
2. Which command starts a login shell?
bash --login forces the shell to behave as a login shell and read the profile files.
3. Where should an exported environment variable meant for all processes typically go?
Exported variables belong in profile files so they are set once at login and inherited by child processes.
Flash Cards
What triggers a login shell? — Authentication: console login, SSH, or su - all create a login shell.
Login shell startup files? — /etc/profile, then the first of ~/.bash_profile, ~/.bash_login, or ~/.profile.
Non-login interactive startup file? — ~/.bashrc (usually after /etc/bash.bashrc).
How to detect a login shell? — shopt -q login_shell, or check if $0 starts with a dash.
Where do aliases belong? — In ~/.bashrc, which the login profile should source explicitly.