Colodebug: A simple way to improve bash script debugging (2021)
9 months ago
- #shell-scripting
- #bash
- #debugging
- Extend GNU bash scripts with a simple method to improve execution flow visibility.
- Use colon comments (:) to enhance xtrace mode with context information.
- Colon comments can be used to add a verbose execution mode to scripts.
- The method is compatible with zsh and other Bourne-style shells, but limited in POSIX sh.
- Replace hash comments (#) with colon comments (:) for better debugging.
- Add a custom : function to enable verbose mode when COLODEBUG is set.
- The custom : function prints debug messages to stderr if the argument starts with ::.
- The method avoids unnecessary overhead by only enabling verbose mode when COLODEBUG is set and xtrace is off.
- Example script provided to demonstrate the technique.
- The technique is licensed under Creative Commons Attribution-NoDerivatives 4.0 International License.