Debugging Bash Scripts

When a Bash script doesn't do what you expect, these techniques show you exactly what it is doing, stop it at the first problem, and keep a record of what happened, even when it runs on its own overnight.

Check for mistakes before you run it

bash -n myscript.sh
Checks the script for syntax errors, such as a missing fi or done, without running it. No output means no syntax errors were found. A missing fi shows something like: line 4: syntax error: unexpected end of file.
shellcheck myscript.sh
ShellCheck finds common bugs, such as missing quotes around variables, and explains how to fix them. Install it with sudo apt install shellcheck, or paste your script into shellcheck.net.

See every command as it runs

bash -x myscript.sh
Runs the script and prints each command, with variables filled in, before it runs. Lines starting with + are the trace.
PS4='+ line ${LINENO}: ' bash -x myscript.sh
Adds the line number to each traced command, so you can see exactly where you are in the script.
set -x
# the commands you want to trace
set +x
Put these lines inside a script to trace just one part of it.

Stop at the first error

By default Bash carries on after a command fails, which can hide the real problem. Add these lines near the top of your script:

#!/bin/bash
set -euo pipefail
trap 'echo "Error on line $LINENO" >&2' ERR
set -e stops the script when a command fails, -u treats an unset variable as an error (often a typo), and -o pipefail makes a pipe fail if any part of it fails. The trap line reports which line failed, for example: Error on line 5.

Test your script after adding set -e. Some commands, such as grep finding no matches, return an error on purpose. Add || true after a command that is allowed to fail.

Print your own messages

echo "Copying $src to $dest" >&2
Prints a message to the error output (>&2), so it shows on screen even when the normal output is sent to a file or another command.
declare -p src dest
Shows the exact contents of variables, including spaces and empty values that echo can hide.

Record a terminal session

script mylogfile
Starts recording everything you type, and everything shown on screen, into mylogfile. Type exit to stop.
script -q -c "./myscript.sh" run.log
Runs one script and records its output in run.log.

Log scheduled scripts with logger

A script run by cron or a timer has no screen to show errors on, so send its output to the system log.

./databasebackup.sh 2>&1 | logger -t databasebackup
Sends the script's output and errors (2>&1) to the system log, tagged databasebackup.
journalctl -t databasebackup
Shows the messages with that tag. Add -f to follow them live.

Give a script its own log file

On systems that use rsyslog, such as Ubuntu, tagged messages can be saved to a separate file. Create /etc/rsyslog.d/30-databasebackup.conf containing:

if $programname == 'databasebackup' then /var/log/databasebackup.log
& stop
Saves messages tagged databasebackup to /var/log/databasebackup.log. The & stop line stops them also being added to /var/log/syslog. Change the tag and file name to suit your script.
sudo systemctl restart rsyslog
Restarts rsyslog so it picks up the new settings.

Quick reference

OptionMeaning
bash -n scriptCheck syntax without running
shellcheck scriptFind common bugs
bash -x scriptTrace each command
set -x / set +xTurn tracing on / off
set -euo pipefailStop at the first error
trap '…' ERRRun a command when something fails
>&2Print to the error output
logger -t tagSend output to the system log