Interview › Scripting (Bash, Groovy)
How do you debug a Bash script (set -x, bash -x, shellcheck)? [Intermediate]
Answer
I debug Bash with bash -x script.sh, set -x around suspicious sections, meaningful logging, ShellCheck, and smaller reproducible inputs. I also customize PS4 to show file, line, and function context in xtrace output.
Technical explanation
set -x prints commands after expansion, which helps reveal quoting and variable issues, but it can leak secrets.
ShellCheck catches many static issues before runtime.
Use trap ERR or explicit error handlers to print context on failures in complex scripts.
Hands-on example
export PS4='+ ${BASH_SOURCE}:${LINENO}:${FUNCNAME[0]:-main}: '
set -x
render_config "$ENVIRONMENT"
set +x
bash -n script.sh # syntax check
shellcheck script.sh # static analysisPreparing for an interview?
Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.
More Scripting (Bash, Groovy) interview questions
- What are exit codes you should use, and what does a non-zero exit signal to a pipeline? [Intermediate]
- What is shellcheck, and what kinds of issues does it catch? [Intermediate]
- How do you check whether a command exists before using it? [Intermediate]
- How do you handle errors and provide a useful message on failure? [Intermediate]
- How do you create a secure temporary file or directory (mktemp)? [Intermediate]
- What is the difference between glob expansion and regex in the shell? [Intermediate]
- What is the trap command, and how do you use it for cleanup on exit? [Intermediate]
- What is process substitution (<(...)), and when is it useful? [Intermediate]