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 analysis
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 is the purpose of the shebang line, and what does #!/bin/bash do? [Basic]
- What is the difference between sh and bash? [Basic]
- How do you make a script executable and run it? [Basic]
- What is the difference between running a script with ./script.sh, bash script.sh, and source script.sh? [Basic]
- What does sourcing a script do differently from executing it? [Basic]
- How do you declare a variable in Bash, and why are spaces around = not allowed? [Basic]
- What is the difference between $var and ${var}? [Basic]
- What is the difference between single quotes and double quotes in Bash? [Basic]