# Shell Scripting for Ops

> Write robust Bash scripts for provisioning, entrypoints and CI steps that fail loudly and are safe to re-run. Free notes, key concepts, resources and a mini...

Canonical: https://www.internhack.in/roadmaps/tracks/devops/shell-scripting
Last updated: 2026-11-28

Write robust Bash scripts for provisioning, entrypoints and CI steps that fail loudly and are safe to re-run.

Beginner · about 10 hours · difficulty 2/5

Learn first: [Linux Command Line and Packages](https://www.internhack.in/roadmaps/tracks/devops/linux-basics)

Even if Python is your main language, you will read and write shell scripts constantly: Dockerfile entrypoints, CI steps, bootstrap scripts and one-off fixes. The goal is scripts that are **safe, idempotent and readable**.

### Building blocks

- Shebang `#!/usr/bin/env bash` and strict mode `set -euo pipefail`.
- Always quote expansions: `"$var"`, `"$@"`.
- Test conditions with `[[ ... ]]`, check commands with `command -v`, compare exit codes with `$?`.
- Functions with `local` variables, and a `main` function called at the bottom.
- `trap cleanup EXIT` for temp files created with `mktemp`.
- Here-docs for generating config files.

### Idempotency

A script that provisions a server should be safe to run twice: check before creating (`mkdir -p`, `id user || useradd user`), and prefer declarative tools once logic grows.

### JSON in the shell

`jq` is essential for reading API responses and cloud CLI output, for example `aws ec2 describe-instances | jq -r '.Reservations[].Instances[].InstanceId'`.

### Pitfalls

Parsing `ls` output, silent failures in pipelines without `pipefail`, and scripts that only work from one directory (use `cd "$(dirname "$0")"`).

**Good enough to move on:** you can write a re-runnable bootstrap script with logging, argument parsing and cleanup that passes ShellCheck.

## Key concepts

- **Strict mode and quoting**
- **Conditionals, loops and functions**
- **Exit codes and error handling**
- **trap and mktemp**
- **Idempotent scripts**
- **jq for JSON**
- **cron scheduling**

## Mini project

Write a bootstrap.sh that creates a user, installs packages, writes a config from a here-doc and enables a service; run it twice on the same VM and prove the second run changes nothing.

## Check yourself

Without set -o pipefail, what exit code does false | true return, and why does that matter in CI?

## Free resources

- [GNU Bash Reference Manual](https://www.gnu.org/software/bash/manual/) (GNU)
- [jq manual](https://jqlang.org/manual/) (jqlang)
- [ShellCheck](https://www.shellcheck.net/) (shellcheck.net)

Previous: [Performance and Troubleshooting](https://www.internhack.in/roadmaps/tracks/devops/linux-performance) · Next: [Text Wrangling: grep, sed, awk and Friends](https://www.internhack.in/roadmaps/tracks/devops/text-processing)

## More in Shell Scripting and Terminal Craft

- [Shell Scripting for Ops](https://www.internhack.in/roadmaps/tracks/devops/shell-scripting)
- [Text Wrangling: grep, sed, awk and Friends](https://www.internhack.in/roadmaps/tracks/devops/text-processing)
- [Editors and Terminal Productivity](https://www.internhack.in/roadmaps/tracks/devops/editors-terminal)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [DevOps Engineer Roadmap](https://www.internhack.in/roadmaps/tracks/devops)
