Skip to content

Bash Scripting Cheat Sheet

Last verified May 2026 — runs in your browser
Bash Scripting Cheatsheet
#!/bin/bash

Shebang (script interpreter)

Basics
set -euo pipefail

Exit on error, undefined vars, pipe fail

Basics
echo "Hello World"

Print line

Basics
read -p "Name: " name

Read user input with prompt

Basics
read -s password

Read silent input (passwords)

Basics
# This is a comment

Comment

Basics
name="Alice"

Assign variable (no spaces!)

Variables
echo "$name"

Read variable

Variables
echo "${name}!"

Brace syntax for clarity

Variables
readonly PI=3.14

Read-only constant

Variables
unset name

Remove variable

Variables
export PATH="$HOME/bin:$PATH"

Export to subshells

Variables
${var:-default}

Default if unset

Variables
${var:=default}

Default and assign if unset

Variables
${var:?error message}

Error if unset

Variables
${#var}

String length

Variables
${var:0:3}

Substring (offset 0, length 3)

Variables
${var/old/new}

Replace first occurrence

Variables
${var//old/new}

Replace all occurrences

Variables
if [ "$x" -eq 1 ]; then
  echo "one"
fi

If with numeric compare

Conditionals
if [[ "$s" == "hi" ]]; then ... fi

If with string compare ([[ ]])

Conditionals
if [ -f file.txt ]; then ... fi

If file exists

Conditionals
if [ -d /etc ]; then ... fi

If directory exists

Conditionals
if [ -z "$x" ]; then ... fi

If string is empty

Conditionals
if [ -n "$x" ]; then ... fi

If string is non-empty

Conditionals
-eq -ne -lt -le -gt -ge

Numeric operators

Conditionals
[[ "$s" =~ ^[0-9]+$ ]]

Regex match

Conditionals
case "$x" in
  1) ... ;;
  2|3) ... ;;
  *) ... ;;
esac

Case statement

Conditionals
[ -f file ] && echo exists || echo missing

Inline if/else

Conditionals
for i in 1 2 3; do echo $i; done

For over list

Loops
for i in {1..10}; do ... done

For over range

Loops
for i in $(seq 1 10); do ... done

For with seq

Loops
for f in *.txt; do ... done

For over files (glob)

Loops
for ((i=0; i<10; i++)); do ... done

C-style for loop

Loops
while [ "$n" -lt 10 ]; do
  n=$((n+1))
done

While loop

Loops
while read line; do echo "$line"; done < file.txt

Read file line by line

Loops
until [ condition ]; do ... done

Until loop (negated while)

Loops
break continue

Break/continue loop

Loops
arr=(a b c d)

Create array

Arrays
echo "${arr[0]}"

Access by index

Arrays
echo "${arr[@]}"

All elements

Arrays
echo "${#arr[@]}"

Array length

Arrays
arr+=(e f)

Append elements

Arrays
for item in "${arr[@]}"; do ... done

Iterate array

Arrays
declare -A map
map[key]="value"

Associative array (dict)

Arrays
greet() {
  echo "Hi $1"
}
greet "Alice"

Define and call function

Functions
$1 $2 $3 ... $@ $#

Args: pos 1, 2, 3, all, count

Functions
local x=5

Function-local variable

Functions
return 0

Exit code from function

Functions
command > file.txt

Redirect stdout (overwrite)

I/O & Pipes
command >> file.txt

Redirect stdout (append)

I/O & Pipes
command 2> errors.log

Redirect stderr

I/O & Pipes
command &> both.log

Redirect stdout + stderr

I/O & Pipes
command < input.txt

Input from file

I/O & Pipes
cmd1 | cmd2 | cmd3

Pipe between commands

I/O & Pipes
$(command)

Command substitution

I/O & Pipes
$((2 + 3))

Arithmetic expansion

I/O & Pipes
cmd1 && cmd2 || cmd3

Run cmd2 if cmd1 succeeds, else cmd3

I/O & Pipes
cmd &

Run in background

I/O & Pipes
wait

Wait for background jobs

I/O & Pipes
trap 'cleanup' EXIT

Run on script exit

I/O & Pipes
Showing 61 of 61 snippets

Bash Cheat Sheet — Variables, Loops, Conditionals & Pipes Reference

Bash has shipped as the default shell on Linux servers since the early nineties, and the core grammar has barely shifted since — variables, conditionals, loops, pipes, file tests. The cheatsheet below covers more than 60 snippets across those areas plus parameter expansion and array operations. Most trouble in real scripts doesn't come from forgetting syntax. It comes from quirks that look ordinary but bite. A stray space around `=` turns an assignment into a command lookup that fails. `set -e` quietly stops protecting the script the moment two commands get chained with a pipe. An unquoted variable holding a value with a space in it splits into separate words and breaks every test downstream. The snippets below are the ones a script author reaches for while debugging exactly that — the right comparison operator, the right expansion, the right redirection — without rereading the Bash manual from line one each time.

Common pitfalls in Bash

A few patterns earn their place on the first screen of any Bash script. `set -euo pipefail` at the top keeps errors in plain sight — the shell exits on the first error, undefined variables count as errors, and pipe failures propagate. It doesn't catch everything, but it catches considerably more than the default `set -e` alone. Variable quoting matters: `"$file"` not `$file`, every single time, because the moment a filename holds a space or a glob character, an unquoted expansion makes a single argument unfold into three. The `[[ ... ]]` form is generally preferable to `[ ... ]` when POSIX portability is not required — it handles empty variables without breaking, supports regular expressions, and behaves more predictably. Bash arithmetic with `$((...))` understands only integers, so fractional math should be delegated to `bc` or `awk` rather than wrestled with inside Bash. The cheatsheet groups all of this into Basics, Variables, Conditionals, Loops, Arrays, Functions, and I/O so the right section is one click away.

  • 60+ Bash scripting snippets
  • Variable expansions and defaults
  • Conditionals and test operators
  • Arrays (indexed and associative)
  • Functions and arguments
  • Pipes and redirection

Free. No signup. Your inputs stay in your browser. Ads via Google AdSense (consent required).

By ·

Related guides