/cheatsheets/bash/bash-cheat-sheet
Bash cheat sheet: variables, tests, loops and expansions
verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated 2026-10-20
- tool
- Bash
- versions
- bash 4.x, bash 5.x
- os
- linux, macos, windows
A compact reference for everyday bash. Every expansion in the first block below was run in bash 5.
Variables and parameter expansion
name="report.tar.gz"
echo "${name%.gz}" # remove shortest suffix
echo "${name%%.*}" # remove longest suffix
echo "${name#*.}" # remove shortest prefix
echo "${#name}" # length
echo "${name/tar/zip}" # replace first match
echo "${missing:-default}" # default if unset or empty
echo "${name^^}" # uppercase (bash 4+)Output
report.tar report tar.gz 13 report.zip.gz default REPORT.TAR.GZ
| Syntax | Meaning |
|---|---|
| ${var:-word} | word if var is unset or empty |
| ${var:=word} | assign word if var is unset or empty |
| ${var:?msg} | exit with msg if var is unset or empty |
| ${var:+word} | word if var is set and non-empty |
| ${var:offset:length} | substring |
| $(command) | command substitution |
| $((a + b)) | arithmetic expansion |
Tests
| Test | True when |
|---|---|
| [[ -f path ]] | path is a regular file |
| [[ -d path ]] | path is a directory |
| [[ -e path ]] | path exists |
| [[ -z $s ]] / [[ -n $s ]] | string is empty / not empty |
| [[ $a == $b ]] | strings are equal (right side is a glob pattern if unquoted) |
| [[ $s =~ regex ]] | string matches the extended regex |
| (( a < b )) | arithmetic comparison |
Loops and conditionals
for f in a b c; do printf '%s ' "$f"; done; echo
for ((i = 1; i <= 3; i++)); do printf '%d ' "$i"; done; echo
n=0; while (( n < 2 )); do echo "n=$n"; ((n++)); done
case "linux" in lin*) echo "matched lin*" ;; *) echo "other" ;; esacOutput
a b c 1 2 3 n=0 n=1 matched lin*
Functions and arrays
greet() { local who="${1:-world}"; echo "hello, $who"; }
greet; greet errcodex
arr=(alpha beta gamma)
echo "${arr[1]} ${#arr[@]} ${arr[@]:1}"
declare -A port=([http]=80 [https]=443)
echo "${port[https]}"Output
hello, world hello, errcodex beta 3 beta gamma 443
Redirection
| Syntax | Meaning |
|---|---|
| cmd > file | stdout to file (overwrite) |
| cmd >> file | stdout to file (append) |
| cmd 2> file | stderr to file |
| cmd > file 2>&1 | stdout and stderr to file |
| cmd &> file | same, bash shorthand |
| cmd < file | file as stdin |
| cmd <<< "text" | here-string as stdin |
| cmd1 | cmd2 | stdout of cmd1 to stdin of cmd2 |
Script header
#!/usr/bin/env bash
set -euo pipefailSee set -euo pipefail for what these options change.
Sources
Related pages
flags · bashset -euo pipefail: what each flag does in bashset -e exits on the first failing command, -u treats unset variables as errors and -o pipefail makes a pipeline fail if any command in it fails. Examples.codes · bashBash exit codes: 0, 1, 2, 126, 127, 128+n and 255What bash exit codes mean: 0 success, 1 general error, 2 misuse of builtins, 126 not executable, 127 not found, 128+n killed by signal n, and how to read $?.glossary · bashWhat is a shebang (#!) line?A shebang is the #! first line of a script that names its interpreter, such as #!/usr/bin/env bash. The kernel reads it when you run the file as a program.