🎨 Chapter 4: Make It Yours — Shell Customisation
⭐ XP: 400 | 🏆 Badge: Shell Customizer
Shell Configuration Files
~/.bashrc # Runs for every interactive non-login shell
~/.bash_profile # Runs for login shells (SSH, console login)
~/.profile # POSIX-compatible; used by sh, dash
/etc/bash.bashrc # System-wide bashrc
/etc/profile # System-wide profile
Aliases — Shortcuts for Long Commands
# Add to ~/.bashrc:
alias ll='ls -la --color=auto'
alias la='ls -la'
alias grep='grep --color=auto'
alias ..='cd ..'
alias ...='cd ../..'
alias update='sudo apt update && sudo apt upgrade -y'
alias df='df -h'
alias free='free -h'
# Apply immediately:
source ~/.bashrc
Environment Variables
echo $PATH # Current PATH
export PATH=$PATH:/opt/myapp/bin # Add to PATH
export EDITOR=nano # Set default editor
export HISTSIZE=5000 # Expand history
printenv # Show all environment variables
env # Also shows environment
The PS1 Prompt
# Customise your prompt in ~/.bashrc:
export PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
# \u = username \h = hostname \w = working directory
# \033[01;32m = bold green \033[00m = reset colour
Shell Functions
# Add to ~/.bashrc:
mkcd() { mkdir -p "$1" && cd "$1"; }
extract() {
case "$1" in
*.tar.gz) tar -xzf "$1" ;;
*.zip) unzip "$1" ;;
*.tar.bz2) tar -xjf "$1" ;;
esac
}
Command History
history # Show command history
history | tail -20 # Last 20 commands
!42 # Re-run command #42
!! # Re-run last command
!ssh # Re-run last command starting with 'ssh'
Ctrl+R # Reverse search history (type to search)
Ctrl+G # Cancel history search
Tab Completion and Shortcuts
Tab # Complete command/file/variable
Tab Tab # Show all completions
Ctrl+A # Go to start of line
Ctrl+E # Go to end of line
Ctrl+K # Delete from cursor to end
Ctrl+U # Delete entire line
Ctrl+W # Delete previous word
Alt+. # Insert last argument of previous command
Lab 4.1 — Build Your ~/.bashrc
cp ~/.bashrc ~/.bashrc.backup # Always backup first!
nano ~/.bashrc
# Add: aliases, PS1, functions, PATH additions
source ~/.bashrc
echo $PS1
type ll # Confirm alias is active
📋 Certification Notes
- Know the difference between login shell vs interactive shell and which files each reads
source fileand. fileare equivalent — run in current shell contextexportmakes a variable available to child processes- PATH order matters: first match wins