Fifteen common shell scripting interview questions
- What does the shebang line do?
- Single quotes versus double quotes?
- Why should you quote variables like "$file"?
- What are exit codes, and how do you check them?
- What do set -e, set -u and set -o pipefail do?
- Explain stdin, stdout, stderr and redirection such as 2>&1.
- How do pipes work?
- How do you loop over files safely, including names with spaces?
- What are $1, $@, $# and $??
- How do you parse command-line options?
- grep versus sed versus awk — when to use each?
- Find the ten largest files in a directory tree.
- Count the most frequent IP addresses in an access log.
- How do you schedule a script to run regularly?
- How do you debug a shell script?
Quoting, variables and exit codes
- Shebang: #!/usr/bin/env bash selects the interpreter.
- Quotes: single quotes are literal; double quotes allow variable and command expansion. Quote variables to prevent word splitting and globbing — an unquoted $file breaks on spaces.
- Special variables: $1 is the first argument, "$@" all arguments as separate words, $# the count, $? the last exit code, $$ the shell's process ID.
- Exit codes: 0 means success, anything else failure; test with if command; then … or command || handle_error.
Safer scripts
| Setting or practice | Effect |
|---|---|
| set -e | Exit when a command fails (with some exceptions in conditionals) |
| set -u | Treat unset variables as errors |
| set -o pipefail | A pipeline fails if any command in it fails |
| trap '…' EXIT | Clean up temporary files on exit |
| mktemp | Create temporary files safely |
| shellcheck | Static analysis that catches common mistakes |
Redirection and pipes
Every process has stdin (0), stdout (1) and stderr (2). > redirects stdout to a file, >> appends, 2> redirects stderr, and 2>&1 sends stderr wherever stdout currently goes — so command > out.log 2>&1 captures both. A pipe connects one command's stdout to the next command's stdin, letting small tools combine. Use while IFS= read -r line to read input line by line safely.
Text processing, with examples
- grep filters lines by pattern: grep -i error app.log.
- sed edits streams: sed 's/old/new/g' file.
- awk processes fields: awk '{print $1}' access.log prints the first field, often the IP.
- Top IPs: awk '{print $1}' access.log | sort | uniq -c | sort -rn | head.
- Largest files: find . -type f -exec du -h {} + | sort -rh | head -n 10.
- Safe file loops: find . -name '*.log' -print0 | while IFS= read -r -d '' f; do …; done.
Arguments, scheduling and debugging
Parse short options with getopts in a while loop and validate required arguments, printing usage on error. Schedule with cron (crontab -e, five time fields) or systemd timers, remembering that cron runs with a minimal environment, so set PATH and use absolute paths. Debug with bash -x script.sh or set -x to trace each command, and shellcheck to catch problems before running. See Linux interview questions.
When not to use a shell script
Interviewers like candidates who know the limits. Shell is excellent for gluing commands together, short automation and system tasks. Once a script needs complex data structures, robust error handling across many steps, JSON processing beyond simple queries, or more than a few hundred lines, a language such as Python is usually easier to test and maintain. Saying when you'd switch — and why — shows judgement, not weakness.
Practising
Interviewers follow up on the most specific thing you say, so rehearse the second question as well as the first. CoPilot Interview, a free AI interview assistant, runs shell scripting mock interviews with follow-ups and produces a report of likely next-round questions. Solve the log-parsing and largest-files tasks from memory, then explain each pipe stage aloud. See also system administrator questions.
Disclosure: CoPilot Interview is our product, so weigh what we say about it accordingly. Everything else on this page applies whichever tool you use, or none.
Frequently asked questions
What are common shell scripting interview questions?
Shebang, quoting, special variables, exit codes, set -euo pipefail, redirection and pipes, safe file loops, argument parsing, grep, sed and awk, scheduling with cron and debugging.
What does 2>&1 mean?
Redirect standard error (file descriptor 2) to wherever standard output (file descriptor 1) is currently going, so both streams end up in the same place.
Why should you quote variables in Bash?
Unquoted variables undergo word splitting and filename expansion, so values containing spaces or wildcards break commands. Quoting keeps each value intact.
What does set -o pipefail do?
It makes a pipeline return a failure status if any command in it fails, instead of only reporting the exit status of the last command.
Practise on real interview questions
Run 145 real interview questions across 10 tracks, answer out loud and see a structured answer to compare with yours. Free, no signup.
Try a practice question