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 31–45 of 570 questions

31.How do you handle errors so the script stops and tells you where?MidBash Scripting

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.

32.How do you loop over files whose names may have spaces?MidBash Scripting

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.

33.How do you parse options like -v and -f file in a script?MidBash Scripting

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.

34.What is a here document, and when would you use one?MidBash Scripting

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.

35.How do you prevent two copies of a cron script running at once?SeniorBash Scripting

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.

36.How do you test and lint Bash scripts in CI?SeniorBash Scripting

Run ShellCheck on every script, write tests with bats-core, run with bash -n for syntax, and test under the shell that will run it (dash for /bin/sh scripts).

37.When should you stop using Bash and switch to Python?SeniorBash Scripting

Switch when the script needs real data structures, JSON/YAML parsing, error handling with retries, unit tests, or grows past roughly 100-200 lines. Bash is great for gluing commands together; Python is easier to read, test and maintain for logic.

38.How do you run commands in parallel in Bash and wait for them?SeniorBash Scripting

Start them with & and call wait; capture each PID to check exit codes (wait $pid). For many jobs, xargs -P 8 or GNU parallel limits how many run at once.

39.How do you make a script safe to run twice (idempotent)?SeniorBash Scripting

Check state before changing it: mkdir -p, id user || useradd user, grep -q line file || echo line >> file. Use lock files (flock) to stop overlapping runs, and write to a temp file then mv it into place so a crash never leaves half a file.

40.How do you keep secrets out of shell scripts and logs?SeniorBash Scripting

Read secrets from environment variables or a secret manager at runtime, never hardcode them. Avoid set -x around secret lines, don't pass secrets as command-line arguments (they show in ps), and use read -s for prompts.

41.What is the difference between git fetch and git pull?BeginnerGit

fetch downloads new commits and updates remote-tracking branches without touching your work. pull is fetch plus merge (or rebase) into the current branch.

42.What is the difference between merge and rebase?BeginnerGit

Merge joins two histories with a merge commit and keeps history as it happened. Rebase replays your commits on top of another branch for a linear history, creating new commit IDs. Never rebase commits others have already pulled.

43.What is the difference between the working directory, the staging area and a commit?BeginnerGit

The working directory is your files as they are now. git add copies changes to the staging area (the index), which is what the next commit will contain. git commit saves the staged snapshot into history.

44.What does .gitignore do, and why doesn't it ignore a file I already committed?BeginnerGit

It tells Git which untracked files to leave out, like node_modules/ or .env. Files that are already tracked keep being tracked; remove them from the index with git rm --cached file, then commit.

45.What is the difference between git reset, git revert and git restore?BeginnerGit

git restore throws away changes in files. git reset moves your branch back to an earlier commit (rewrites local history). git revert makes a new commit that undoes an old one, which is safe on shared branches.