Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

Using Bash set -x to Help Debug Why a Script Isn't Working

using-bash-set-x-to-help-debug-why-a-script-is-not-working.jpg

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:

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.

# Demo Video

Timestamps

  • 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 -x helps 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 $* and $@ with Bash
  • 9:09 – Why I like naming variables instead of using $1, $2 and $@
  • 10:28 – Using set -x in your Bash script to apply bash -x by default
  • 11:28 – Does it with work POSIX complaint shell scripts?

Reference Links

When was the last time -x helped you debug something? Let me know below.

Never Miss a Tip, Trick or Tutorial

Like you, I'm super protective of my inbox, so don't worry about getting spammed. You can expect a few emails per year (at most), and you can 1-click unsubscribe at any time. See what else you'll get too.



Comments