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; }
doneCheck 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
- How do you write a dry-run mode into an automation script, and why is it valuable? [Advanced]
- What is the difference between Groovy and Java for scripting purposes? [Advanced]
- How do you run stages in parallel in a Jenkins Groovy pipeline? [Advanced]
- How do you handle errors and retries in a Groovy pipeline (try/catch, retry, timeout)? [Advanced]
- How do you define and call a function (or step) in a Jenkins Groovy pipeline? [Advanced]
- How do you securely handle secrets in a shell script (avoid hardcoding, use env or a vault)? [Advanced]
- What is idempotency, and how do you make a script safe to run repeatedly? [Advanced]
- What is the difference between a cron job and a systemd timer? [Advanced]