...

/

Debugging

Debugging

In this lesson, you will learn about bash flags useful for debugging, how to trace bash code, and useful resources for linting bash scripts.

How Important is this Lesson?

If you write, use or maintain bash of any complexity you’ll want to know how to debug it!

Syntax Checking Options

Start by creating this simple (but broken) script:

Press + to interact
cat > debug_script.sh << 'END'
#!/bin/bash
A=some value
echo "${A}
echo "${B}"
END
Terminal 1
Terminal
Loading...

Now run it with the -n flag. This flag only parses the script, rather than running it. It’s useful for detecting basic syntax errors.

Press + to interact
bash -n debug_script.sh

You can see it’s broken. Fix it. Then run it:

Press + to interact
bash debug_script.sh

You’ll see:

    [1]+  Done                    sleep 60

in the terminal.

Again, it reports the job number, this time with the status (Done), and the command that was originally run (sleep 60).

Controlling Jobs

Just like starting jobs, you can control jobs by sending signals to them.

Here you’re going to start two jobs, one to sleep for two minutes, and the next for one second more (so we can distinguish between them).

Press + to interact
bash -v debug_script.sh

Try tracing to see more details about ...

Access this course and 1400+ top-rated courses and projects.