All cheat sheets

Bash Cheat Sheet

Quick reference for Bash: variables, redirection, pipes, conditionals, loops, and one-liners.

CLI & Shell
bash
shell
cli

Bash is a command interpreter built around composing small programs with expansion, pipelines, and exit statuses. Quote data by default, inspect failures explicitly, and prefer readable scripts over clever one-liners.

Variables and quoting

Assignments have no spaces around =. Parameter expansion reads a value, while double quotes preserve it as one argument and prevent word splitting and pathname expansion.

bash
name="Ada Lovelace"
printf 'Hello, %s\n' "$name"
count=${#name}
readonly VERSION="1.0"
Table
FormMeaning
VAR=valueSet a shell variable for the current shell.
export VARPass the variable to child processes.
${VAR:-default}Use default when VAR is unset or empty.
${VAR:?message}Stop with message when VAR is unset or empty.
"$VAR"Preserve whitespace and suppress glob expansion.
'$VAR'Treat $VAR literally; single quotes do no expansion.
`cmd`Legacy command substitution; prefer $(cmd).
bash
file="report final.txt"
if [[ -f "$file" ]]; then
  printf 'Found: %s\n' "$file"
fi

Special parameters

Bash exposes useful information about the current command, process, and script arguments. $@ should normally be quoted as "$@" so each original argument remains separate.

Table
ParameterMeaning
$?Exit status of the most recent pipeline or command.
$!PID of the most recent background job.
$0Script or shell name.
$1$9Positional arguments.
$@All arguments, preserving boundaries when quoted.
$#Number of positional arguments.
$$PID of the current shell.
$-Current shell option flags.
bash
printf 'script=%s args=%s\n' "$0" "$#"
for arg in "$@"; do
  printf 'arg=%q\n' "$arg"
done

Redirection and pipes

Redirections are processed by the shell before a command runs. File descriptor 0 is standard input, 1 is standard output, and 2 is standard error.

Table
SyntaxEffect
cmd > fileWrite stdout, replacing the file.
cmd >> fileAppend stdout.
cmd < fileRead stdin from a file.
cmd 2> fileWrite stderr separately.
cmd 2>&1Send stderr to the current stdout destination.
cmd &> fileSend stdout and stderr to one file in Bash.
cmd1 | cmd2Pipe stdout of cmd1 into stdin of cmd2.
cmd | tee fileDisplay output and save a copy.
bash
make 2>&1 | tee build.log
printf '%s\n' "$(< input.txt)" > output.txt

Use set -o pipefail in scripts when a failure inside a pipeline should make the whole pipeline fail.

Conditionals and tests

[[ ... ]] is Bash’s safer conditional expression syntax. It supports pattern matching and avoids many word-splitting surprises found with the older [ ... ] command.

bash
if [[ -z ${1:-} ]]; then
  printf 'usage: %s FILE\n' "$0" >&2
  exit 2
elif [[ -r $1 && -f $1 ]]; then
  printf 'Readable regular file\n'
else
  printf 'Not a readable regular file\n' >&2
fi
Table
TestChecks
-e pathPath exists.
-f pathRegular file.
-d pathDirectory.
-r/-w/-x pathReadable, writable, or executable.
-n stringNon-empty string.
-z stringEmpty string.
a == bString equality inside [[ ]].
n -gt 3Numeric comparison.

Loops

Use for to iterate over words or arrays, while for a condition, and until for the inverse condition. Quote file paths and use -- where a command supports it.

bash
for file in *.log; do
  [[ -e $file ]] || continue
  printf '%s: ' "$file"
  wc -l < "$file"
done

while IFS= read -r line; do
  printf '%s\n' "$line"
done < input.txt
bash
for ((i = 1; i <= 3; i++)); do
  printf 'attempt %d\n' "$i"
done

Practical one-liners

These recipes favor portable shell tools and explicit quoting. Replace the placeholder paths and patterns before running them on important data.

bash
find . -type f -name '*.tmp' -print -delete
bash
find . -type f -name '*.txt' -exec sed -i.bak 's/old/new/g' {} +
bash
printf '%s\n' *.log | wc -l
bash
while IFS= read -r file; do
  printf '%s\n' "$file"
done < <(find . -type f -print)

References