Learn Bash – tutorial, cheatsheet & exercises
Bash is the shell on almost every Linux server, in Docker containers, in CI/CD pipelines and (alongside zsh) on the Mac. You control the computer through text commands, chain small programs together with pipes and automate recurring tasks: backups, log analysis, deployments, file renaming.
Bash scripts are ideal as "glue" between existing tools. The language has its quirks, though: everything is text, spaces are separators, and quoting decides whether your script is correct. This course therefore emphasizes the typical pitfalls and a robust style (quoting, set -euo pipefail, trap). Note: for licensing reasons macOS still ships Bash 3.2 – features like associative arrays need Bash 4+ (brew install bash).
Hello World in Bash
#!/usr/bin/env bash
echo "Hello World!"echo prints text. The shebang line determines that Bash runs the script; then chmod +x hallo.sh and ./hallo.sh.
How to get started with Bash
Open a terminal: macOS Applications → Utilities → Terminal (or Spotlight "Terminal"), Linux Ctrl+Alt+T, Windows install WSL (wsl --install in PowerShell) or use Git Bash. Check your version and whether you are in Bash or zsh:
bash --version
echo "$SHELL" # login shell (macOS: /bin/zsh, Linux usually /bin/bash)
echo "$BASH_VERSION" # only set when you are currently in BashA script is a text file. The first line, the shebang, tells the system which interpreter runs it. #!/usr/bin/env bash finds Bash via the PATH and is more portable than #!/bin/bash. With chmod +x you make the file executable.
cat > hallo.sh <<'EOF'
#!/usr/bin/env bash
echo "Hello from the script!"
EOF
chmod +x hallo.sh # make executable
./hallo.sh # run it (./ = in the current folder)
bash hallo.sh # also works without chmod +xRecommended tools: an editor with shell support (VS Code with the "ShellCheck" extension, or nano/vim in the terminal) and ShellCheck (brew install shellcheck, apt install shellcheck), a static analyzer that automatically finds quoting mistakes and traps. An empty folder is ideal for safe practice:
mkdir -p ~/bash-uebung && cd ~/bash-uebung
shellcheck hallo.sh # check the script (if installed)
bash -n hallo.sh # syntax check only, run nothing
bash -x hallo.sh # show each line before it runs (debugging)Bash learning path
Terminal, Navigation & Files
Change folders, create, copy, move, delete and read files.
Pipes & Redirection
Chain programs and direct input/output into files.
Variables, Quoting & Arithmetic
Store values, set quotes correctly, command output and arithmetic.
Conditions: if, [[ ]] and case
Branching, file and text tests, comparisons and pattern matching.
Loops: for, while, until
Work through lists, number ranges, files and lines.
Functions
Reusable building blocks with arguments, local variables and return values.
Script Arguments & getopts
$1, $@, $#, shift, default values and parsing options.
grep, sed, awk & Co.
Filter, replace and analyze text – the tools for logs and CSV.
find, xargs & Bulk File Processing
Find files by name, age and size and apply commands to them.
Exit Codes, Error Handling & set -euo pipefail
Reliable scripts: detect errors, abort and clean up.
Arrays, Associative Arrays & Parameter Expansion
Handle lists safely and split strings without external programs.
Here-Docs, Process Substitution & Debugging
Embed multi-line text, treat command output like files and debug scripts.
Processes, Jobs, Signals & trap
Background jobs, parallel execution, catching signals and cleaning up properly.
Script Best Practices & Portability
Robust, maintainable scripts: template, quoting, ShellCheck and the limits of Bash.
Automation with cron & a Practical Project
Run scripts on a schedule: crontab syntax, environment, logs and locks.
At a glance
More about Bash
Test & practice
Interactive quiz, timed exam with certificate, and exercises with an editor: