Objective

Understand the difference between login and non-login shells, which startup files run in each case, and how to correctly configure your shell environment.

Tools & Technologies

  • ~/.bashrc
  • ~/.bash_profile
  • ~/.profile
  • /etc/profile
  • login shell
  • non-login shell

Key Commands

source ~/.bashrc
echo $BASH_VERSION
bash --login
bash --noprofile
type -a ls

Architecture Overview

sequenceDiagram participant LS as Login Shell participant NL as Non-Login Interactive participant ETC as /etc/profile participant BP as ~/.bash_profile participant RC as ~/.bashrc LS->>ETC: Source /etc/profile ETC-->>LS: System vars set LS->>BP: Source ~/.bash_profile BP->>RC: Often sources ~/.bashrc RC-->>BP: Aliases + functions BP-->>LS: Profile complete NL->>RC: Source ~/.bashrc directly RC-->>NL: Aliases + functions

Step-by-Step Process

01
Login vs Non-Login Shells

A login shell runs when you SSH in or use su - user. A non-login interactive shell runs when you open a terminal emulator.

# Check if current shell is login
echo $0          # login shells start with -
# e.g.: -bash (login)  vs  bash (non-login)

# Test: start explicit login shell
bash --login

# What ran at startup?
bash -x --login 2>&1 | head -30
02
Which File to Edit

Put environment variables (export) in .bash_profile/.profile. Put aliases and functions in .bashrc.

# ~/.bash_profile – runs for LOGIN shells
export PATH=$PATH:~/bin
export EDITOR=vim

# Bridge: source .bashrc from .bash_profile
[ -f ~/.bashrc ] && source ~/.bashrc

# ~/.bashrc – runs for INTERACTIVE non-login
alias ll='ls -lah'
export PS1='[\u@\h \W]\$ '
03
Debugging Startup

Use -x tracing to see exactly what runs during shell startup.

# Trace login shell startup
bash -x --login 2>&1 | head -50

# Find where a command is defined
type -a ls
type -a ll

# Check all sourced files manually
grep -r 'source\|\.' ~/.bash_profile ~/.bashrc 2>/dev/null

Challenges & Solutions

  • .bashrc is not sourced in login shells unless .bash_profile explicitly sources it
  • Breaking .bashrc with a syntax error can make new terminals fail to open

Key Takeaways

  • ~/.profile is the POSIX-compatible alternative to .bash_profile
  • Always test .bashrc changes with source ~/.bashrc before opening a new terminal