Interview › Scripting (Bash, Groovy)
How do you make a script portable across different shells and systems? [Advanced]
Answer
To make a script portable, I declare the intended shell, avoid non-portable features when targeting sh, check required commands, use POSIX-compatible options where possible, avoid hardcoded paths, handle GNU versus BSD differences, and test in the target environments.
Technical explanation
If the script requires Bash features, say so with the shebang and version checks instead of pretending it is portable sh.
Use command -v, mktemp, POSIX flags, and defensive quoting to reduce environment assumptions.
Containerized test runs are useful for validating behavior on Ubuntu, Alpine, RHEL-like images, and macOS where relevant.
Hands-on example
#!/usr/bin/env bash
set -euo pipefail
if (( BASH_VERSINFO[0] < 4 )); then
echo "Bash 4+ required" >&2
exit 2
fi
for cmd in awk sed curl; do
command -v "$cmd" >/dev/null || { echo "missing $cmd" >&2; exit 127; }
done
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]