Shell and Command Line Interface
What is a Shell?
The Shell is the command-line interpreter bridging the user and the operating system kernel. Every command entered by the user is parsed by the Shell, translated into a sequence of system calls, and submitted to the kernel for execution.
User Input ──▶ Shell Parsing ──▶ System Calls ──▶ Kernel Execution
"ls -la" Parse args fork+exec Read dir & return
Think of the Shell as an interpreter: you speak in human-readable commands, and it translates them into the binary logic (system calls) the kernel inherently understands.
Common Shell Architectures
| Shell | Description |
|---|---|
| Bash | The ubiquitous, default Shell for most Linux distributions. |
| Zsh | An enhanced iteration of Bash, famously the default on macOS. |
| Fish | A modern Shell boasting syntax highlighting and aggressive auto-completion. |
| sh | The ancestral Bourne Shell, serving as the baseline POSIX standard. |
| Dash | A highly lightweight, POSIX-compliant Shell (used by Debian/Ubuntu for system scripts). |
# Verify the current active Shell
echo $SHELL # Output: /bin/bash
# View all installed Shells supported by the system
cat /etc/shells
File and Directory Operations
Navigation and Inspection
pwd # Print Working Directory
ls -la # List all files (including hidden), detailed output
cd /path/to/dir # Change directory
cd ~ # Return to the user's home directory
cd - # Return to the previously visited directory
tree -L 2 # Visualize directory structure as a tree, depth of 2
File Manipulation
# Creation
touch file.txt # Create an empty file / Update timestamp
mkdir -p a/b/c # Recursively create parent and child directories
# Copy / Move / Delete
cp -r src/ dst/ # Recursively copy a directory
mv old.txt new.txt # Rename or Move
rm -rf dir/ # Force recursive deletion (⚠️ DANGER: Irreversible)
# Content Inspection
cat file.txt # Dump entire content to stdout
less file.txt # Paginated viewing (supports searching)
head -n 20 file.txt # Inspect the first 20 lines
tail -f log.txt # Real-time tailing (essential for log monitoring)
Text Processing and Search
grep — Text Search Engine
grep "error" log.txt # Search for lines containing 'error'
grep -i "error" log.txt # Case-insensitive search
grep -r "TODO" src/ # Recursive search through directories
grep -n "pattern" file.txt # Display line numbers alongside matches
grep -c "error" log.txt # Count matching lines
grep -v "debug" log.txt # Invert match (exclude 'debug' lines)
find — Filesystem Search
find / -name "*.log" # Search by filename
find . -size +100M # Locate files larger than 100MB
find . -mtime -7 # Locate files modified within the last 7 days
find . -type f -name "*.tmp" -delete # Locate and immediately delete
awk / sed — Stream Processing
# awk: Columnar text processing
awk '{print $1, $3}' file.txt # Print the 1st and 3rd columns
awk -F: '{print $1}' /etc/passwd # Print usernames using ':' as delimiter
awk '$3 > 100 {print $0}' data.txt # Print lines where the 3rd column > 100
# sed: Stream Editor
sed 's/old/new/g' file.txt # Global replacement
sed -i '3d' file.txt # Delete the 3rd line (in-place modification)
sed -n '10,20p' file.txt # Print lines 10 through 20
Pipes and Redirection
The Pipe |
Pipes redirect the standard output (stdout) of one command directly into the standard input (stdin) of the next, enabling powerful command chaining:
# Count the total number of files in the current directory
ls -l | wc -l
# Identify the top 5 memory-consuming processes
ps aux | sort -k4 -rn | head -5
# Real-time monitoring of ERROR logs
tail -f app.log | grep --line-buffered "ERROR"
Redirection
# Standard Output (stdout) Redirection
echo "hello" > file.txt # Overwrite file
echo "world" >> file.txt # Append to file
# Standard Error (stderr) Redirection
command 2> error.log # Redirect stderr to file
command 2>&1 # Merge stderr into stdout
command > all.log 2>&1 # Redirect all output to a single file
# /dev/null — The Black Hole
command > /dev/null 2>&1 # Silent execution; discard all output
File Descriptors:
0 ── stdin (Standard Input) ← Keyboard
1 ── stdout (Standard Output) → Terminal
2 ── stderr (Standard Error) → Terminal
Process Management
# Process Inspection
ps aux # View all processes (BSD syntax)
ps -ef # View all processes (POSIX syntax)
top # Real-time monitor (CPU, Memory utilization)
htop # Enhanced, visually intuitive alternative to top
# Process Termination
kill PID # Send SIGTERM (Graceful termination)
kill -9 PID # Send SIGKILL (Force kill, cannot be intercepted)
killall nginx # Terminate all instances by process name
# Background Execution
command & # Run in the background
nohup command & # Run in the background, immune to terminal hangups
jobs # List background jobs in current session
fg %1 # Bring background job 1 to the foreground
Networking Commands
# Network State
ifconfig / ip addr # View network interfaces and IPs
ping host # Test ICMP network connectivity
traceroute host # Map the routing path to a host
curl url # Emit HTTP requests
wget url # Download files
# Ports and Connections
netstat -tlnp # List actively listening TCP ports
ss -tlnp # Modern, faster alternative to netstat
lsof -i :8080 # Identify which process holds port 8080
Shell Scripting Fundamentals
Script Architecture
#!/bin/bash # Shebang: Defines the interpreter
# This is a comment
# Variables (No spaces around the equals sign)
name="World"
echo "Hello, $name!"
# Command-line Arguments
echo "Script name: $0"
echo "First argument: $1"
echo "Arg count: $#"
echo "All args: $@"
echo "Exit code of last command: $?"
Conditionals
# if-else logic
if [ -f "file.txt" ]; then
echo "File exists"
elif [ -d "dir" ]; then
echo "Is a directory"
else
echo "Does not exist"
fi
# Common conditional flags
# -f : Regular file exists -d : Directory exists
# -e : Entity exists -z : String is empty
# -n : String is not empty -eq: Numerically equal
# -gt: Greater than -lt: Less than -ge: Greater or equal
Loops
# for loop
for file in *.log; do
echo "Processing: $file"
done
# while loop
count=0
while [ $count -lt 5 ]; do
echo "Count: $count"
count=$((count + 1))
done
# Reading a file line-by-line
while IFS= read -r line; do
echo "$line"
done < input.txt
Functions
# Function Definition
greet() {
local name=$1 # 'local' scopes the variable to the function
echo "Hello, $name!"
return 0 # Explicit exit code
}
# Function Invocation
greet "Alice" # Output: Hello, Alice!
System Design Audit & Observability
When building or debugging infrastructure, mastering Shell commands is critical for observing system state.
1. Tracing Port Exhaustion and Bind Failures
When a service fails to start due to Address already in use, you must deterministically identify the rogue process holding the socket.
- Audit Command:
lsof -i :<port>orss -tlnp | grep <port>. These will expose the PID and process name holding the bind lock, allowing targeted termination.
2. Streamlining High-Volume Log Analysis
Parsing gigabytes of production logs requires stream processing, not opening files in memory.
- Audit Command: Combine
tail -fwithgrep --line-bufferedto trace active errors without memory bloat. For historical forensic analysis, utilizeawkto extract specific payload fields rather than dumping raw JSON rows.
3. Idempotent Scripting Boundaries
In CI/CD or automation scripts, shell scripts must be bulletproof against state corruption.
- Audit Protocol: Ensure scripts utilize the Shebang correctly (
#!/bin/bash), leverageset -e(exit on error), and use>vs>>correctly to prevent unintentional file corruption or disk exhaustion via runaway appends.