Skip to content
$ lynuxpro

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–15 of 20 questions

Learn Bash Scripting →
1.What does set -euo pipefail do?Beginner

-e exits on any failing command, -u fails on unset variables, and pipefail makes a pipeline fail if any stage fails, not just the last. Together they stop scripts from silently carrying on after an error.

2.Why should you quote variables, like "$file"?Beginner

Unquoted variables undergo word splitting and glob expansion, so a filename with spaces or a * breaks the command or matches other files. Quoting keeps the value as one argument.

3.What is the difference between single and double quotes in Bash?Beginner

Single quotes keep everything literally, so '$HOME' prints $HOME. Double quotes still expand variables and $(...) but stop word splitting and globbing, which is why you use "$var" almost everywhere.

4.How do you check if a command succeeded?Beginner

Every command sets an exit code in $?: 0 means success. Usually you test it directly: if grep -q ERROR app.log; then ... or chain with cmd && next || fallback.

5.How do you pass arguments to a script and use them?Beginner

Arguments arrive as $1, $2 and so on; $# is how many there are and "$@" is all of them, each kept separate. Check them early, for example [[ $# -ge 1 ]] || { echo "usage: $0 file"; exit 1; }.

6.What is the difference between [ ] and [[ ]]?Beginner

[ is the old POSIX test command, so variables must be quoted and && doesn't work inside it. [[ ]] is Bash syntax: safer with empty variables, supports &&, ||, pattern matching and regex with =~. Use [[ ]] in Bash scripts.

7.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.

8.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.

9.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.

10.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.

11.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.

12.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.

13.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.

14.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.

15.How do you prevent two copies of a cron script running at once?Senior

Use a lock: exec 9>/var/lock/job.lock; flock -n 9 || exit 0. The kernel releases the lock when the process exits, even if it crashes, unlike a hand-made PID file.