INTERVIEW PREP
Interview questions
570 questions from real DevOps, SRE, platform engineering and MLOps interviews, with the answer an interviewer wants to hear. Try answering out loud first, then open the model answer.
Showing 1–8 of 8 questions
Learn Bash Scripting →1.How do you make sure a temporary file is always cleaned up?Mid
Create it with mktemp and register trap 'rm -f "$tmp"' EXIT. The trap runs however the script ends, including errors under set -e.
2.What is the difference between $@ and $*?Mid
Quoted, "$@" expands to each argument as a separate word, preserving spaces inside arguments; "$*" joins all arguments into one string. Use "$@" to pass arguments on.
3.How do you read a file line by line safely?Mid
while IFS= read -r line; do ...; done < file. IFS= keeps leading/trailing whitespace, -r keeps backslashes, and redirecting into the loop avoids a subshell so variables set inside survive.
4.How do you write a function that returns a value?Mid
return only sets an exit code from 0 to 255. To return data, print it and capture it: get_ip() { hostname -I | awk '{print $1}'; } then ip=$(get_ip). Use local for variables inside functions.
5.How do you handle errors so the script stops and tells you where?Mid
Use set -euo pipefail, plus trap 'echo "error on line $LINENO" >&2' ERR. Check commands whose failure is OK explicitly (cmd || true) and send error messages to stderr.
6.How do you loop over files whose names may have spaces?Mid
Use a glob, not ls: for f in /data/*.csv; do process "$f"; done. With find, use find . -name '*.log' -print0 | while IFS= read -r -d '' f; do ...; done.
7.How do you parse options like -v and -f file in a script?Mid
Use the getopts builtin: while getopts 'vf:' opt; do case $opt in v) verbose=1;; f) file=$OPTARG;; *) usage;; esac; done. Then shift $((OPTIND-1)) leaves the remaining arguments.
8.What is a here document, and when would you use one?Mid
A here document feeds several lines to a command: cat <<'EOF' > config.yml ... EOF. Quoting 'EOF' stops variable expansion, which is handy for writing config files or sending a block of commands over SSH.