Bash Scripting Quick Reference
==============================

VARIABLES
  NAME="value"           Set variable (no spaces around =)
  echo $NAME             Use variable
  echo "${NAME}_suffix"  Variable in string
  readonly VAR="val"     Constant

CONDITIONALS
  if [ condition ]; then
    commands
  elif [ condition ]; then
    commands
  else
    commands
  fi

  # String comparisons
  [ "$a" = "$b" ]        Equal
  [ "$a" != "$b" ]       Not equal
  [ -z "$a" ]            Empty string
  [ -n "$a" ]            Non-empty string

  # Numeric comparisons
  [ $a -eq $b ]          Equal
  [ $a -ne $b ]          Not equal
  [ $a -lt $b ]          Less than
  [ $a -gt $b ]          Greater than

  # File tests
  [ -f file ]            Regular file exists
  [ -d dir ]             Directory exists
  [ -r file ]            Readable
  [ -x file ]            Executable

LOOPS
  for i in 1 2 3; do echo $i; done
  for f in *.txt; do cat "$f"; done
  for ((i=0; i<10; i++)); do echo $i; done
  while read line; do echo "$line"; done < file
  while true; do cmd; sleep 1; done

FUNCTIONS
  myfunc() {
    echo "Arg1: $1, Arg2: $2"
    return 0
  }
  myfunc "hello" "world"

ARRAYS
  arr=(one two three)
  echo ${arr[0]}          First element
  echo ${arr[@]}          All elements
  echo ${#arr[@]}         Length

STRING OPERATIONS
  ${#var}                 String length
  ${var:0:5}              Substring (offset:length)
  ${var/old/new}          Replace first match
  ${var//old/new}         Replace all matches
  ${var%.ext}             Remove suffix
  ${var#prefix}           Remove prefix

SPECIAL VARIABLES
  $0                      Script name
  $1, $2, ...             Arguments
  $#                      Number of arguments
  $@                      All arguments
  $?                      Last exit code
  $$                      Current PID
  $!                      Last background PID

USEFUL PATTERNS
  cmd || echo "failed"    Run on failure
  cmd && echo "ok"        Run on success
  $(command)              Command substitution
  $((1 + 2))              Arithmetic
  cmd &                   Background process
