Bash Cheat Sheet

Searchable reference for everyday Bash commands.

Open tool

Overview

A searchable reference covering the Bash commands and constructs you actually use day to day - file ops, text processing, process management, redirection, conditionals, loops, and the most-cited shortcuts from man bash (history expansion, brace expansion, parameter substitution). Group by category or type any keyword to filter.

It's for developers who write shell scripts every few weeks but never quite memorise the obscure flags. Reach for it when you need to recall whether [[ ]] supports =~, the exact substring syntax (${var:offset:length}), or how to splice elements out of an array.

How it works

Entries map to features documented in the GNU Bash reference manual, organised by purpose rather than command name. Each entry includes a short example showing the working syntax and any notable POSIX vs Bash-only behaviour. Parameter expansions and tests are flagged where they differ in dash, zsh, or strict POSIX shells.

The list focuses on what's idiomatic in modern Bash 4+ (associative arrays, mapfile, lastpipe) while noting features that work all the way back to Bash 3.2 (still shipped on stock macOS).

Examples

  • String length and slice:
    s="hello world"
    echo ${#s}        # 11
    echo ${s:6:5}     # world
    
  • Iterate over files matching a glob:
    for f in *.log; do
      gzip "$f"
    done
    
  • Conditional with regex match:
    if [[ $email =~ ^[^@]+@[^@]+$ ]]; then
      echo "looks like an email"
    fi
    
  • Capture command output into a variable:
    branch=$(git rev-parse --abbrev-ref HEAD)
    

FAQ

Does this cover POSIX sh or only Bash?

It covers Bash, with notes where a construct ([[ ]], arrays, local) won't work in POSIX sh. If you're targeting dash or BusyBox, prefer the POSIX-flagged examples.

Why use [[ ]] instead of [ ]?

[[ ]] is a Bash builtin with no word splitting or glob expansion on unquoted variables, and it supports =~ and < / > lexical compares. [ ] is the older POSIX form.

What's the difference between $() and backticks?

Functionally the same, but $() nests cleanly without escape gymnastics. Prefer it for new code.

How do I make my script fail fast?

set -euo pipefail near the top: exit on any error, on unset variables, and on failures inside pipelines. Add IFS=$'\n\t' to make word splitting predictable.

Try Bash Cheat Sheet

An unhandled error has occurred. Reload ×