Back to roadmaps shell-script Course

Shell Scripting Reference Cheat Sheet

This quick reference guide covers essential Bash syntax and common one-liner utilities.


1. Special Variables

Variable Meaning
$0 Script filename
$1 ... $9 Positional arguments
$# Number of arguments
$@ All arguments as list
$? Exit code of last command
$$ Current process PID
$(command) Command substitution output

2. File and String Test Operators

# File tests
[ -f file ]   # File exists and is a regular file
[ -d dir ]    # Directory exists
[ -r file ]   # File is readable
[ -z "$var" ] # String is empty (zero length)
[ -n "$var" ] # String is not empty

3. Useful One-Liners

# Find top 10 disk-consuming directories
du -sh */ | sort -rh | head -10

# Monitor log file in real time
tail -f /var/log/app.log

# Kill all processes matching a name
pkill -f "node server.js"

# Count lines in a file
wc -l filename.txt

# Download a file
curl -O https://example.com/file.zip

# Check if a service is running
systemctl is-active --quiet nginx && echo "running" || echo "stopped"
Published on Last updated: