# debugging bash scripts #bash #debug for debugging your script just add this line `trap 'echo "# $BASH_COMMAND";read' DEBUG` to the top of the script after the shebang line example: ```shell #!/bin/bash trap 'echo "# $BASH_COMMAND";read' DEBUG echo line1 echo line2 echo line3 ``` - run script - before each command runs you will see what will be running - for run press ENTER - To stop running CTRL+C ![[CleanShot 2024-10-22 at 01.07.10.gif]] What is going on here: - **trap**, which can intercept different signals, and in our case it intercepts the **DEBUG** signal sent before command execution. - $BASH_COMMAND is a special variable that holds the current command being executed. So, this echo will print the command about to be executed, prefixed with # to make it look like a comment. - The **read** command, which knows how to wait for something to be entered from the keyboard (in this case we only need either ENTER or Ctrl+C)