Quick Reference

Cheatsheets

Practical command references for Linux, networking, servers, containers, databases, and more.

Cheatsheet#bash-cheatsheet

Bash Cheatsheet

Bash (Bourne Again SHell) is the default shell on most Linux distributions.

Basics

#!/bin/bash             # Shebang, always put this at the top of scripts
chmod +x script.sh      # Make script executable
./script.sh             # Run the script

Variables

name="John"             # No spaces around the equals sign!
echo $name              # Output: John
echo "Hello $name"      # Variable interpolation (double quotes)
echo 'Hello $name'      # Literal string (single quotes, no interpolation)
readonly PI=3.14        # Constant variable
unset name              # Delete variable

Special Variables

$0      # Name of the script
$1-$9   # Arguments 1 to 9
$#      # Number of arguments
$@      # All arguments as a list
$?      # Exit status of the last command (0 = success)
$$      # Process ID (PID) of the current shell
$USER   # Current user
$PWD    # Current working directory

Parameter Expansion

${var:-default}   # Use 'default' if var is unset or empty
${var:=default}   # Set var to 'default' if unset or empty
${#var}           # Length of string
${var%pattern}    # Remove shortest match from end
${var#pattern}    # Remove shortest match from start
${var/find/rep}   # Replace first match
${var//find/rep}  # Replace all matches

Arrays

fruits=("Apple" "Banana" "Cherry")
echo ${fruits[0]}         # First element: Apple
echo ${fruits[@]}         # All elements
echo ${#fruits[@]}        # Number of elements
fruits+=("Orange")        # Append to array

Control Flow (if/else)

if [ "$name" == "John" ]; then
    echo "It is John"
elif [ "$name" == "Doe" ]; then
    echo "It is Doe"
else
    echo "Who are you?"
fi

Common Conditions

  • -z "$var" : True if string is empty
  • -n "$var" : True if string is not empty
  • "$a" == "$b" : True if strings are equal
  • "$a" != "$b" : True if strings are not equal
  • -eq, -ne, -lt, -le, -gt, -ge : Numeric comparisons
  • -f file : True if file exists and is a regular file
  • -d dir : True if directory exists
  • -e file : True if file/directory exists

Loops

# For loop
for i in {1..5}; do
    echo "Number: $i"
done
 
# Iterate over files
for file in *.txt; do
    echo "Found text file: $file"
done
 
# While loop
count=1
while [ $count -le 5 ]; do
    echo $count
    ((count++))
done

Functions

greet() {
    local name=$1    # Local variable
    echo "Hello, $name!"
}
 
greet "World"        # Output: Hello, World!

Redirection & Pipes

command > file.txt      # Redirect stdout to file (overwrite)
command >> file.txt     # Redirect stdout to file (append)
command 2> error.log    # Redirect stderr to file
command > file 2>&1     # Redirect stdout and stderr to same file
command &> file         # Redirect stdout and stderr (shorthand)
command < file.txt      # Read input from file
command1 | command2     # Pipe stdout of command1 to stdin of command2

Job Control

ctrl+c      # Kill current foreground job
ctrl+z      # Suspend current foreground job
jobs        # List jobs
bg %1       # Resume job 1 in background
fg %1       # Bring job 1 to foreground