Interview › Scripting (Bash, Groovy)
How do you check whether a command exists before using it? [Intermediate]
Answer
I check whether a command exists with command -v name >/dev/null 2>&1. It is portable and avoids relying on which, which may be external and inconsistent.
Technical explanation
Validate dependencies at script startup so failures are clear and early.
Use a loop for multiple required tools and print installation guidance if helpful.
For optional tools, use command -v to choose a fallback path.
Hands-on example
require() {
command -v "$1" >/dev/null 2>&1 || {
echo "required command not found: $1" >&2
exit 127
}
}
for cmd in kubectl jq curl; do
require "$cmd"
donePreparing 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 is shellcheck, and what kinds of issues does it catch? [Intermediate]
- What is the difference between glob expansion and regex in the shell? [Intermediate]
- What are exit codes you should use, and what does a non-zero exit signal to a pipeline? [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 trap command, and how do you use it for cleanup on exit? [Intermediate]
- What is an associative array, and how do you use one? [Intermediate]
- What is process substitution (<(...)), and when is it useful? [Intermediate]