Using Bash set -x to Help Debug Why a Script Isn't Working
Bash has an x flag or you can use set -x in a script to print a trace of commands and variables. It's really handy!
Quick Jump: Demo Video
If you’re wondering why a variable isn’t outputting what you expect or why a command you’re trying to run isn’t working as expected then running
bash -x [YOUR_SCRIPT] or defining
set -x in your script might be exactly what you need.
In this video we’ll see how both work and how they help to debug shell scripts.
- 0:08 – Going over the demo script really quick
- 1:07 – Running the script to demonstrate the error
- 2:23 – It worked manually but not in the script, hmm
- 3:11 – How
bash -xhelps us find the problem within seconds
- 5:16 – A few ways to solve this specific error around passing in arguments
- 6:17 – Making ShellCheck happy
- 6:58 – Parsing arguments and one difference between
- 9:09 – Why I like naming variables instead of using
- 10:28 – Using
set -xin your Bash script to apply
bash -xby default
- 11:28 – Does it with work POSIX complaint shell scripts?
When was the last time -x helped you debug something? Let me know below.