← Linux from ScratchCheat sheet
M11Linux from Scratch

M11 — Shell startup files

Time: about 40–60 minutes
Lab root: /tmp/linux-lab-pushpjeet (we will practice safely; your real ~/.bashrc gets only a harmless alias)

Prerequisites#

  • M10 (variables, export, temporary PS1)
  • Comfort editing a text file in vim (M7) or another editor

Learning objectives#

By the end of this module you will be able to:

  1. Explain which bash files run for login vs interactive shells (at a practical level)
  2. Add a harmless customisation to ~/.bashrc
  3. Reload settings with source ~/.bashrc (or . ~/.bashrc) without logging out
  4. Avoid common startup-file footguns (breaking PATH, infinite loops, editing the wrong file)

Why startup files exist#

Every new terminal should feel like your workshop: aliases, PATH tweaks, a prompt you like. Bash reads a small set of files when it starts. Put durable settings there instead of retyping export every morning.


The files you will hear about#

Names vary slightly by distro, but this mental model works on most Linux systems:

File Typical role
~/.bashrc Interactive non-login shells (most terminal windows)
~/.bash_profile or ~/.profile Login shells (SSH login, console login)
/etc/bash.bashrc or /etc/bashrc System-wide defaults (need admin rights)

On many desktop setups, the login profile sources ~/.bashrc, so editing .bashrc covers both. On others, you may need a one-line source in .bash_profile. Check what you have:

ls -la ~/.bashrc ~/.bash_profile ~/.profile 2>&1

Example from a lab machine:

ls: cannot access '/home/box/.bash_profile': No such file or directory
-rw-r--r-- 1 box box 3526 Jul 12 22:28 /home/box/.bashrc
-rw-r--r-- 1 box box  807 Jul 12 22:28 /home/box/.profile

Your dates and sizes will differ. The point: know which files exist before you edit.

Peek at the top of .bashrc (comments only — do not copy vendor training text; just read your own file):

head -n 8 ~/.bashrc

You will usually see a note that this file is for non-login interactive shells, plus guards so non-interactive scripts do not load the whole rc file.


Login vs interactive (practical version)#

  • Interactive: you type at a prompt (a terminal tab).
  • Login: the shell is the one created when you log in (SSH often counts).

For day-to-day customisation on a laptop GUI terminal, ~/.bashrc is the right first stop. For SSH-only servers, still start with .bashrc, and ensure your login profile sources it if aliases seem “missing” after SSH.

SSH login shell
   └─ reads ~/.bash_profile or ~/.profile
         └─ often contains:  source ~/.bashrc
               └─ your aliases / exports

Edit safely: additive changes only#

Rules of the road:

  1. Back up first: cp ~/.bashrc ~/.bashrc.bak-$(date +%Y%m%d)
  2. Add at the bottom — do not delete large chunks you do not understand
  3. Prefer aliases and exports over clever magic until you are comfortable
  4. Reload with source, then test, before opening twenty tabs

Harmless alias used in this course:

# --- pushpjeet linux-from-scratch lab ---
alias ll='ls -la'

How to apply:

# 1) backup
cp ~/.bashrc ~/.bashrc.bak-lfs

# 2) append alias if not already present
grep -q "alias ll=" ~/.bashrc || echo "alias ll='ls -la'" >> ~/.bashrc

# 3) reload
source ~/.bashrc

# 4) verify
type ll
ll /tmp/linux-lab-pushpjeet | head

Expected idea: ll is an alias for ls -la, and the listing shows detailed file metadata.

To undo later: restore the backup or delete that alias line, then source ~/.bashrc again.


source vs starting a new shell#

Action Effect
source ~/.bashrc or . ~/.bashrc Re-read the file in the current shell
Open a new terminal tab New shell reads startup files fresh
bash -c '…' Usually non-interactive — may skip .bashrc

If an alias “does not work” in a script, that is normal: scripts are non-interactive. Use full commands in scripts.


What belongs in .bashrc (beginner list)#

Good early candidates:

  • alias ll='ls -la'
  • alias ..='cd ..'
  • export EDITOR=vim
  • small PATH additions you understand: export PATH="$HOME/bin:$PATH"

Leave alone for now: complex prompt frameworks, untrusted pasted blobs from the internet, anything that runs network commands at startup.


Common mistakes#

  1. Editing /etc/bashrc as a beginner — stay in your home directory files.
  2. Syntax error in .bashrc — new shells may act oddly. Fix with bash --noprofile --norc if needed, then restore the backup.
  3. Putting exit in a startup file — your shell opens and closes instantly. Remove that line.
  4. Forgetting source — you edited the file but the current session still has old settings.
  5. Assuming .bash_profile always exists — many systems use .profile instead; list before you invent files.

Hands-on lab#

Important: Only append a harmless alias. Do not wipe your rc file.

  1. Back up: cp ~/.bashrc ~/.bashrc.bak-lfs
  2. Append alias ll='ls -la' if missing (see commands above).
  3. source ~/.bashrc
  4. Run ll /tmp and confirm long listing format.
  5. Open a new terminal tab and run ll again — it should still work.
  6. Optional cleanup: restore ~/.bashrc.bak-lfs if you do not want to keep the alias.

Verify: type ll reports an alias.


Practice: check your understanding#

Multiple choice#

1. Which file is the usual place for aliases on an interactive desktop terminal?

  • A. /etc/passwd
  • B. ~/.bashrc
  • C. /tmp/linux-lab-pushpjeet
  • D. ~/.ssh/known_hosts
Answer

B. ~/.bashrc holds interactive shell customisations for most users.

2. What does source ~/.bashrc do?

  • A. Deletes the file
  • B. Emails the file to root
  • C. Executes the file in the current shell
  • D. Compiles bash from source
Answer

C. source (same as .) runs the file now, in this shell.

3. Why back up .bashrc before editing?

  • A. Bash requires two copies
  • B. A typo can disrupt new interactive shells
  • C. Backups increase PATH speed
  • D. Aliases cannot be removed otherwise
Answer

B. A broken rc file makes every new terminal painful; a backup is cheap insurance.

4. Login shells often read which of these first?

  • A. Only /tmp
  • B. ~/.bash_profile or ~/.profile
  • C. vimrc only
  • D. HISTORY
Answer

B. Login shells typically start with a profile file, which may source .bashrc.

5. An alias defined only in .bashrc may not appear in a non-interactive script because:

  • A. Scripts ban the letter l
  • B. Non-interactive bash usually skips .bashrc
  • C. Aliases work only as root
  • D. source is illegal in scripts
Answer

B. Scripts are non-interactive; they generally do not load your interactive rc customisations.

Order the steps#

Add and verify a harmless ll alias:

  1. source ~/.bashrc
  2. cp ~/.bashrc ~/.bashrc.bak-lfs
  3. Append alias ll='ls -la' to ~/.bashrc
  4. Run type ll and ll /tmp
Answer key

2 → 3 → 1 → 4
(backup → edit → reload → verify)

Mini terminal challenge#

Goal: Add a second harmless alias and prove reload works.

Setup: Working bash and your own ~/.bashrc (with backup).

Tasks

  1. Backup .bashrc.
  2. Add alias la='ls -A' at the bottom (skip if it already exists).
  3. Without closing the terminal, source ~/.bashrc.
  4. Run la /tmp/linux-lab-pushpjeet.

Verify: type la shows an alias; listing includes entries that ls without -A might hide (such as dotfiles if present).

Stretch: Document in a course note when you would put the same alias in .bashrc vs exporting a variable.


Next: M12 — Permissions, ownership, umask — read ls -l, change modes safely, and understand umask.

Continue

← Linux from Scratch hub · Cheat sheet · All tutorials