Prerequisites#
- M10 (variables,
export, temporaryPS1) - Comfort editing a text file in vim (M7) or another editor
Learning objectives#
By the end of this module you will be able to:
- Explain which bash files run for login vs interactive shells (at a practical level)
- Add a harmless customisation to
~/.bashrc - Reload settings with
source ~/.bashrc(or. ~/.bashrc) without logging out - 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:
- Back up first:
cp ~/.bashrc ~/.bashrc.bak-$(date +%Y%m%d) - Add at the bottom — do not delete large chunks you do not understand
- Prefer aliases and exports over clever magic until you are comfortable
- 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
PATHadditions 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#
- Editing
/etc/bashrcas a beginner — stay in your home directory files. - Syntax error in
.bashrc— new shells may act oddly. Fix withbash --noprofile --norcif needed, then restore the backup. - Putting
exitin a startup file — your shell opens and closes instantly. Remove that line. - Forgetting
source— you edited the file but the current session still has old settings. - Assuming
.bash_profilealways exists — many systems use.profileinstead; list before you invent files.
Hands-on lab#
Important: Only append a harmless alias. Do not wipe your rc file.
- Back up:
cp ~/.bashrc ~/.bashrc.bak-lfs - Append
alias ll='ls -la'if missing (see commands above). source ~/.bashrc- Run
ll /tmpand confirm long listing format. - Open a new terminal tab and run
llagain — it should still work. - Optional cleanup: restore
~/.bashrc.bak-lfsif 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_profileor~/.profile - C.
vimrconly - 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.
sourceis 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:
source ~/.bashrccp ~/.bashrc ~/.bashrc.bak-lfs- Append
alias ll='ls -la'to~/.bashrc - Run
type llandll /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
- Backup
.bashrc. - Add
alias la='ls -A'at the bottom (skip if it already exists). - Without closing the terminal,
source ~/.bashrc. - 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.