This website requires JavaScript.

Bash

Variables

name="John"
echo $name      # Output: John
readonly name   # Make variable read-only
unset name      # Delete variable

Strings

str="Hello World"
echo ${#str}        # Length
echo ${str:0:5}     # Substring
echo ${str/World/Bash} # Replace

Arrays

arr=(apple banana cherry)
echo ${arr[1]}          # Access
arr[2]="grape"          # Modify
echo ${arr[@]}          # All elements
echo ${#arr[@]}         # Length

Conditionals

if [ $a -gt $b ]; then
  echo "a > b"
elif [ $a -eq $b ]; then
  echo "a = b"
else
  echo "a < b"
fi

Loops

For Loop
While Loop
  for i in 1 2 3; do
    echo $i
  done

Functions

greet() {
  echo "Hello, $1"
}
greet "Alice"

Command Substitution

now=$(date)
echo "Current time: $now"

File Test Operators

[ -f file.txt ]   # True if file exists and is regular file
[ -d /dir ]       # True if directory exists
[ -x script.sh ]  # True if file is executable
[ -r file.txt ]   # True if file is readable
[ -w file.txt ]   # True if file is writable

Arithmetic

a=5
b=3
let sum=a+b
echo $sum

Or

echo $((a + b))

Input / Output

read name
echo "Hello, $name"

Redirection

command > file.txt     # Stdout to file
command >> file.txt    # Append stdout
command 2> error.txt   # Stderr to file
command &> all.txt     # Stdout + Stderr to file

Pipes

ls -l | grep ".txt"

Process Management

ps aux          # List processes
kill -9 PID     # Kill process
top             # Real-time process monitor

Job Control

command &       # Run in background
jobs            # List jobs
fg %1           # Bring job to foreground
bg %1           # Continue job in background

File Permissions

chmod +x script.sh      # Make executable
chmod 755 script.sh     # rwxr-xr-x
chown user:group file   # Change owner

Curl & Wget

curl https://example.com
wget https://example.com

Networking

ifconfig         # Show network interfaces (deprecated)
ip addr          # Recommended replacement
ping google.com
netstat -tuln    # List open ports

Archives

tar -czvf file.tar.gz folder/   # Compress
tar -xzvf file.tar.gz           # Extract
zip -r file.zip folder/         # Zip folder
unzip file.zip                  # Unzip

Package Managers (Debian/Ubuntu)

sudo apt update
sudo apt install package
sudo apt remove package

Searching

grep "text" file.txt
find . -name "*.sh"
locate filename

History & Shortcuts

history             # Show command history
!!                  # Repeat last command
!n                  # Repeat nth command
Ctrl + R            # Reverse search
Ctrl + C            # Cancel
Ctrl + D            # Exit

Script Header

#!/bin/bash
# above should be first line of script

File Compression

gzip file.txt            # Compress file.txt to file.txt.gz
gunzip file.txt.gz       # Decompress .gz file
bzip2 file.txt           # Compress to .bz2
bunzip2 file.txt.bz2     # Decompress .bz2 file
xz file.txt              # Compress to .xz
unxz file.txt.xz         # Decompress .xz file

Disk Usage

df -h                   # Show disk usage in human-readable form
du -sh *                # Show sizes of all files/folders in current dir
du -ah | sort -rh       # Sort files by size

Date & Time

date                    # Current date and time
date "+%Y-%m-%d"        # Format date output
date -d "next monday"   # Get date of next Monday

Environment Variables

echo $HOME              # Show home directory
export VAR=value        # Set env variable
env                     # List environment variables
unset VAR               # Unset variable

Shell Expansion

echo *                  # All files in current dir
echo {1..5}             # 1 2 3 4 5
echo {a,b,c}.txt        # a.txt b.txt c.txt

Backups

cp file{,.bak}          # Quick copy to file.bak
rsync -av folder/ backup/   # Sync folders with backup

Crontab (Scheduled Tasks)

crontab -e              # Edit user crontab
crontab -l              # List scheduled jobs
crontab -r              # Remove crontab

Crontab Syntax

# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of week (0 - 6) (Sunday=0)
# │ │ │ │ │
# * * * * * <command to execute>

Example

0 9 * * * /path/to/script.sh     # Run at 9 AM daily
CSS