shBash

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).

AutomationDevOpsServer administrationCI/CD

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 Bash

A 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 +x

Recommended 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)

At a glance

First released1989
CreatorBrian Fox (GNU)
ParadigmImperative, scripting language
TypingDynamic (everything is text)
ExecutionInterpreted (shell)
File extension.sh

Test & practice

Interactive quiz, timed exam with certificate, and exercises with an editor:

Bash compared

Hello World: Bash comparedVariables: Bash comparedConditionals: Bash comparedLoops: Bash comparedFunctions: Bash comparedLists and Arrays: Bash comparedDictionaries and Maps: Bash comparedClasses and Objects: Bash comparedError Handling: Bash comparedReading Files: Bash compared