A Bash script must always include a detailed header block before the actual code. This should include:
- Script Name: Unique name of the script.
- Author: Responsible person(s). (usually Turukmoorea)
- Contact: Email address or other contact option. (usually mail@turukmoorea.ch)
- Repository Link: Link to a public repository. (usually https://github.com/Turukmoorea/...)
- Last Update Date: Date of the last modification.
- License and Terms of Use: Legal notes on usage. (usually unlicense license)
- Detailed Function Description: Purpose, features, requirements.
- Usage: Examples for calls, options, parameters.
- Global Variables: List and explain all global variables with example values and recommended defaults.
- Dependencies: List all external tools, modules, or files required.
The header may be multilingual, but all further comments must be written in English only.
-
Each relevant block must have single or multi-line English block comments:
# ------------------------------------------------------------ # This block performs input validation for user arguments. # It ensures no empty values are passed to the main logic. # Edge cases are handled by rejecting invalid inputs. # ------------------------------------------------------------
-
Non-trivial single lines should be commented inline:
echo "$result" # Output the computed result to stdout
-
Comments should always explain:
- What the block does.
- Why it is implemented this way.
- Possible side effects or constraints.
Each function must have a detailed English docstring block and should be kept as granular as possible to improve code clarity, even if each function is only slightly separated.
# ------------------------------------------------------------
# Function: cleanup
# Purpose : Remove temporary files and restore system state.
#
# Description:
# Called automatically on script exit or interruption.
# Deletes debug artefacts and closes open file descriptors.
#
# Parameters:
# None
#
# Returns:
# None
#
# Globals:
# TMP_DIR - Location of temporary debug files.
#
# Notes:
# Must be robust to handle partial execution states.
# -------------------------------------------------------------
Logging must use a consistent
log_messagefunction. -
Supported log levels: DEBUG, INFO, NOTICE, WARNING, ERROR.
-
Debug mode raises logging level to DEBUG.
-
Logging output can go only to a logfile or optionally be mirrored live to the console using a
verboseflag. -
Failing commands must be logged with the exact command, exit status, and debug information.
-
Verified Logging Only: Never log assumptions or unverified statements. Any success message must be validated by explicitly checking the command’s exit status or output before logging it as successful. Example:
if command_that_should_succeed; then log_message "INFO" "Command succeeded." else log_message "ERROR" "Command failed with exit status $?." exit 1 fi
- Logging must always reflect the actual state, not an expectation.
- This avoids misleading log entries and ensures debugging remains traceable and reliable.
-
Strict safety setup:
set -euo pipefail-e: Exit script on any error.-u: Treat unset variables as errors.pipefail: Failures in pipelines are correctly propagated.
-
Cleanup using trap:
trap cleanup EXIT INT TERM -
cleanupsafely stores debug artefacts such as RAM files, logs, and symlinks.
-
Reproducible debug folders must include:
- Linked original files
- Temporary copies
- Persistent directories as symlinks or copies
- Complete log files
-
Always use a numerically sorted structure (
01_,02_, ...) for clarity.
-
Reusable functions like
log_messageshould be included modularly usingsource:source <(curl -s https://example.org/bash-modules/logging.sh)
-
This keeps the main script readable and modularly maintainable.
-
Mandatory
cleanupfunction: Every script must implement a robustcleanupfunction, which is always triggered bytrapon script exit (EXIT), interruption (INT) or termination (TERM). Example:trap cleanup EXIT INT TERMThe
cleanupfunction must safely handle:- Deletion of all temporary files and debug artefacts.
- Restoration of any system state changes.
- Final log message confirming all resources were released.