Pcornel
Linux · Shell & Bash

Shell Scripts

Cheat sheet · https://pcornel.dk/topic.php?topic=2694

Back to the Topic

Answer

A shell script is a text file of shell commands with a shebang line (#!/bin/bash) and execute permission, used to automate tasks.

Remember

Make it executable (chmod +x), start with set -euo pipefail, quote variables, use full paths in cron, and check with shellcheck. Windows line endings (\r) cause "bad interpreter" errors — fix with dos2unix.

First checks

  • file script.sh — detects CRLF line endings
  • bash -x script.sh — trace each expanded command
  • bash -n script.sh — syntax check only
  • shellcheck script.sh — common bugs and quoting problems
  • echo $? — exit code of the last command

Verify in this order

file with shebang and +x → kernel starts the interpreter → commands run in order → exit code returned

Where it usually breaks

First mismatch in persistent intent → provider/parser/generator → kernel/userspace runtime → dependency/security context → logs/counters → consumer-visible read-back; preserve identity/native diagnostic/pre-state before mutation.

Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=2694

Confirmation

Confirm action

Continue?