Have you ever found yourself staring at a bash script, wondering why on earth it’s not working? Or maybe you’ve inherited someone else’s script and have no idea where to even begin debugging it. Fear not, dear reader! In this blog post, we’ll go over some common pitfalls and how to troubleshoot them, so you can get back to automating all the things.
Key Takeaways
- Debugging bash scripts can be challenging, but there are tools and techniques you can use to make it easier.
- Pay attention to error messages and the exit codes of commands, as they can often provide valuable clues about what’s going wrong.
- Use shell builtins like
set -x
andset -e
to help you debug and catch errors. - Test your script in small increments, rather than trying to fix everything at once.
- Don’t be afraid to ask for help or search online for solutions to specific problems.
Examples and Tips
Use set -x
to turn on debugging output. This will cause the script to print out each command as it’s executed, along with the expanded values of any variables. For example:
#!/bin/bash
set -x
echo "Debugging output for the script at itvraag.nl"
FOO=bar
echo "Value of FOO is $FOO"
Output:
+ echo 'Debugging output for the script at itvraag.nl'
Debugging output for the script at itvraag.nl
+ FOO=bar
+ echo 'Value of FOO is bar'
Value of FOO is bar
Use set -e
to make the script exit immediately upon encountering an error. This can be helpful when you want to catch errors early on and avoid having the script continue to run with potentially unintended consequences. For example:
#!/bin/bash
set -e
echo "This script will exit immediately if any command returns a non-zero exit code"
# This command will succeed, so the script will continue to run
ls itvraag.nl
# This command will fail, causing the script to exit
ls non-existent-file
echo "This line will not be reached because the script exits on the previous line"
Output:
This script will exit immediately if any command returns a non-zero exit code
ls: cannot access 'non-existent-file': No such file or directory
Use bash -x
to run a script with debugging output turned on. This is similar to using set -x
within the script, but allows you to turn on debugging for a script that you don’t have the ability to modify. For example:
bash -x /path/to/script.sh
Use bash -n
to check the syntax of a script without actually running it. This can be useful for catching syntax errors before you try to run the script and encounter an error. For example:
bash -n /path/to/script.sh
Use echo
and printf
to print out variables and other information as you’re debugging. This can help you see what’s going on inside your script and track down problems. For example:
#!/bin/bash
echo "Debugging output for the script at itvraag.nl"
FOO=bar
echo "Value of FOO is $FOO"
printf "The current working directory is %s\\n" "$PWD"
Output:
Debugging output for the script at itvraag.nl
Value of FOO is bar
The current working directory is /path/to/current/directory
Additional Resources
man bash
: The bash man page contains a wealth of information about the shell, including details on all of the builtin commands and options.help
: Typinghelp
at the bash prompt will give you a list of all of the builtin commands, along with a brief description of each. You can also typehelp <command>
to get more information about a specific builtin.
Challenge
Now it’s your turn to try your hand at troubleshooting bash scripts! Here’s a simple script that’s supposed to print out the numbers 1 through 10, but it has a bug that’s causing it to fail. Can you figure out what the problem is and fix it?
#!/bin/bash
for i in {1..10}
do
echo "$i
done
Output:
1
2
3
4
5
6
7
8
9
{10}