Bash as Your First Language

Going deep on Bash first gets you productive on servers quickly, as long as you know when to switch to a real language.

Beginner · about 15 hours · difficulty 2/5

Some people start with Bash as their main automation language because every Linux box already has it and most operational glue (install scripts, entrypoints, CI steps) is written in it. This path is valid, but Bash has sharp edges, so pair it with a general-purpose language later.

What to learn

  • Variables, quoting rules, if, case, for, while, functions and exit codes.
  • Safe script headers: set -euo pipefail and a sensible IFS.
  • Arrays, parameter expansion (${var:-default}), and command substitution.
  • Reading arguments with getopts, and trap for cleanup.
  • Linting with ShellCheck on every script.

When Bash stops being the right tool

Once a script needs data structures, JSON manipulation beyond jq, retries with backoff, or more than a couple of hundred lines, rewrite it in Python or Go. Unquoted variables and word splitting are the number one source of Bash bugs.

Good enough to move on: your scripts pass ShellCheck, fail loudly, clean up temporary files, and you can explain why "$@" is quoted.

Key concepts

  • Quoting and word splitting
  • set -euo pipefail
  • Functions, arrays and expansion
  • getopts and trap
  • ShellCheck

Other options for this step

  • Python for Automation: Python is the most common glue language in operations work: readable, batteries-included and supported by every cloud SDK.
  • Go for Tooling: Go compiles to a single static binary and is the language most cloud-native tools (Docker, Kubernetes, Terraform) are written in.
  • JavaScript / Node.js for Automation: If you already know JavaScript, Node.js is a practical automation language with strong SDKs and a huge package ecosystem.

Mini project

Write a backup script that tars a directory, names it with a timestamp, keeps only the last 7 archives, cleans up on Ctrl+C with trap, and passes ShellCheck with no warnings.

Check yourself

What goes wrong when you write rm -rf $DIR/ without quotes and DIR is empty?

Free resources