Shell Fundamentals & Scripting
Mastering shell interpreters: bash vs. zsh, configuration files (.zshrc, .bashrc), PATH variable, aliases, and shell scripting basics.
Shell Fundamentals & Scripting
The shell is a command language interpreter that executes commands read from standard input devices such as keyboards or from script files.
1. Shell Configuration Files (.zshrc / .bashrc)
When you open a new terminal session, the shell reads startup configuration files located in your home directory (~):
- Zsh (macOS & modern Linux):
~/.zshrc - Bash (Linux server default):
~/.bashrcand~/.bash_profile
Whenever you add custom aliases, export environment variables, or update your PATH, edit this file and reload it:
# Reload changes in current session:
source ~/.zshrc2. The PATH Environment Variable
When you run a command like git or node, the shell does not search your entire hard drive. Instead, it looks in the colon-separated directory list stored in the $PATH variable:
echo $PATH
# Output: /opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbinAdding a Custom Binary to PATH
# Add to ~/.zshrc or ~/.bashrc:
export PATH="$HOME/.local/bin:$PATH"3. Shell Aliases
Aliases create convenient shortcuts for long or frequently typed commands:
# Add to ~/.zshrc:
alias gs="git status"
alias gco="git checkout"
alias pdev="pnpm dev"
alias dc="docker compose"4. Writing a Basic Shell Script
Shell scripts automate multi-step developer routines. Always include a shebang (#!/usr/bin/env bash) on line 1:
#!/usr/bin/env bash
# Strict mode: fail immediately if any command returns non-zero error
set -euo pipefail
echo "🚀 Starting database backup routine..."
BACKUP_DIR="./backups"
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
FILENAME="$BACKUP_DIR/db_$TIMESTAMP.sql"
mkdir -p "$BACKUP_DIR"
if [ -f ".env" ]; then
echo "Found .env file. Proceeding with export..."
# pg_dump ...
echo "✅ Backup successfully saved to $FILENAME"
else
echo "❌ Error: .env file missing!" >&2
exit 1
fiMaking the Script Executable
chmod +x backup.sh
./backup.shRelated Topics
Terminal Fundamentals
Core terminal concepts: terminal emulator vs. shell, standard keyboard shortcuts, piping, redirection, and essential CLI navigation.
Environment Variables and .env
Managing configuration via environment variables: process.env, .env files, security boundaries, and Twelve-Factor App principles.