Interview Prep/Shell Scripting/ShellCheck, debugging, testing, portability and security

How do you debug a Bash script?

BeginnerConceptShellCheck, debugging, testing, portability and security
Answer
bash -n script.sh            # syntax check only, nothing runs
bash -x script.sh            # trace every command after expansion
set -x; risky_part; set +x   # trace just one section
bash -v script.sh            # print lines as read (before expansion)

Read the + lines: they show the command after expansion, so you see exactly what an unquoted variable turned into. Combine with PS4 for file and line numbers, and with trap ... ERR for the failing line.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on ShellCheck, debugging, testing, portability and security