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

Preparing 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

← All Scripting (Bash, Groovy) questions