Shell Startup Files and Login vs Non-Login Shells
I mapped Bash's startup file precedence so customizations apply predictably whether a shell is login, non-login, or interactive. Sourcing .bashrc from .bash_profile guaranteed one canonical place for aliases and environment, avoiding "works in one terminal but not another" bugs.
Objective & Context
Startup file confusion causes environment drift between SSH sessions, terminals, and cron. This lab clarifies which files run for which shell type and establishes a single source of truth, making the aliases-env customizations persistent.
Environment & Prerequisites
- Bash with a writable home directory.
- Access to login and non-login shells (SSH vs new terminal).
- An environment change to test propagation.
flowchart TB
L{Login shell?} -->|yes| BP[.bash_profile]
BP --> SRC[source .bashrc]
L -->|no, interactive| RC[.bashrc directly]
Step-by-Step Execution
1. Source .bashrc from .bash_profile
echo '[ -f ~/.bashrc ] && . ~/.bashrc' >> ~/.bash_profile2. Put customizations in .bashrc
echo "alias ll='ls -lAh'" >> ~/.bashrc && source ~/.bashrc3. Confirm a login shell picks it up
bash -lc 'type ll'll is aliased to `ls -lAh'
Validation & Testing
Open a non-login interactive shell and a login shell and confirm the same alias/environment exists in both. Pass criteria: customizations apply consistently across login and non-login shells with no duplication.
Advanced: Troubleshooting
- Works in terminal, not SSH: SSH starts a login shell; ensure .bash_profile sources .bashrc.
- Duplicated PATH: guard appends so re-sourcing doesn't stack entries.
- cron lacks env: cron uses a minimal shell; set variables explicitly in the job.
Key Results
- Established one canonical file for shell customization.
- Eliminated environment drift between login and non-login shells.
- Verified consistent behaviour across SSH and local terminals.
- Prevented PATH duplication on repeated sourcing.