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.shChecks 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.shShellCheck 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.shRuns 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 +xPut 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' ERRset -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" >&2Prints 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 destShows the exact contents of variables, including spaces and empty values that echo can hide.
Record a terminal session
script mylogfileStarts recording everything you type, and everything shown on screen, into mylogfile. Type exit to stop.
script -q -c "./myscript.sh" run.logRuns 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 databasebackupSends the script's output and errors (2>&1) to the system log, tagged databasebackup.
journalctl -t databasebackupShows 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 & stopSaves 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 rsyslogRestarts rsyslog so it picks up the new settings.
Quick reference
| Option | Meaning |
|---|---|
bash -n script | Check syntax without running |
shellcheck script | Find common bugs |
bash -x script | Trace each command |
set -x / set +x | Turn tracing on / off |
set -euo pipefail | Stop at the first error |
trap '…' ERR | Run a command when something fails |
>&2 | Print to the error output |
logger -t tag | Send output to the system log |