Bash cheat sheet

Bash helps automate repetitive DevOps tasks quickly.

On this page

Table of contents

Use this page for fast recall. Use Full documentation when you want the complete end-to-end path.

Quick workflow

A simple 5-step flow you can follow when using Bash in real work.

1) Setup

Install/run the tool and confirm version. Create a minimal config.

2) Small change

Do one small action end-to-end to prove the workflow.

3) Validate

Check output, logs, and status. Catch mistakes early.

4) Automate

Convert it into a repeatable script or pipeline step.

5) Productionize

Add safety: secrets, rollback, observability, and docs.

Workflows you will actually reuse

These are practical sequences you can copy into your own checklist or runbook.

Turn a manual checklist into automation

Goal: Create a script you can trust in production-like tasks.

- Start by writing the manual steps as comments.

- Add checks (required env vars, command existence).

- Add `set -euo pipefail` and clear error messages.

- Log what you do (`echo` or `logger`) and capture outputs.

- Test on a throwaway environment, then version-control it.

Key Concepts

- Variables

- Conditionals

- Loops

Learning path (high-level):

- Script basics

- Input/output handling

- Automation scripts

Quick Start

Create script

Command

touch script.sh && chmod +x script.sh

Run script

Command

./script.sh

Debug script

Command

bash -x script.sh

Common Commands

Short descriptions and practical intent. Search, filter, copy, and reuse.

Showing 15

Safety
set -euo pipefail

Safer scripts: fail fast + catch unset vars.

Basics
echo "$VAR"

Print variable value (quote to preserve spaces).

Basics
export VAR=value

Set environment variable.

Basics
VAR=${VAR:-default}

Default a variable if unset/empty.

Conditionals
if [ -f file ]; then echo ok; fi

Run logic when file exists.

Conditionals
if command -v rg >/dev/null; then ...; fi

Check if a command exists.

Loops
for i in *; do echo "$i"; done

Loop over files.

Loops
while read -r line; do ...; done < file

Read file line by line.

Pipes
command1 | command2

Pipe output into another command.

I/O
cmd > out.txt 2>&1

Redirect stdout+stderr to a file.

I/O
cmd | tee out.txt

See output and save it.

Text
grep -n "text" file

Search text with line numbers.

Text
awk '{print $1}' file

Extract columns quickly.

Text
jq '.items[] | .name' file.json

Parse JSON on CLI.

Files
chmod +x script.sh

Make script executable.

Copyable snippets

Small blocks you can drop into your terminal, config, or runbook.

Safe script skeleton

bash

#!/usr/bin/env bash
set -euo pipefail

# usage: ./script.sh <arg>
ARG=${1:-}
if [ -z "$ARG" ]; then
  echo "missing arg" >&2
  exit 1
fi

echo "ok: $ARG"

Troubleshooting checklist

When things break, follow this order to stay calm and move fast.

- If a script behaves strangely: run with `bash -x script.sh` and add `set -x` temporarily.

- If pipes hide errors: use `set -o pipefail`.

- If whitespace breaks variables: quote expansions like "$VAR".

Pitfalls

The common mistakes that slow people down when using Bash.

- Copy-pasting commands without understanding inputs/outputs and side effects.

- Not documenting defaults (ports, paths, credentials) and then getting stuck in prod.

- Skipping logs and metrics when troubleshooting; always collect evidence first.

Mini lab (practice)

Do these tasks in order. You will feel the tool instead of just reading about it.

- Install or run the tool locally (or in Docker) and verify it works with a hello-world action.

- Create a minimal config and run the most common command 3 times (with a small change each time).

- Break something on purpose and document how you debugged it in your Notes.

Interview prompts

Use these to test if you truly understand the basics (and can explain them clearly).

- Explain the tool’s role in a real CI/CD pipeline from commit to production.

- Describe the most common failure you’ve seen with this tool and how you fixed it.

- What would you monitor/alert on for this tool in production?

Official Docs

https://www.gnu.org/software/bash/manual/