Home › Blog › Shell scripting interview questions (Bash) with examples

Shell scripting interview questions (Bash) with examples

Shell scripting interviews test whether you can automate tasks safely on Linux and Unix systems. Expect questions on variables and quoting, exit codes, conditionals and loops, pipes and redirection, text processing with grep, sed and awk, functions, argument handling and error handling — plus a short live task such as finding the largest files or parsing a log. Interviewers watch for quoting mistakes and missing error handling, the two most common sources of broken scripts.

Fifteen common shell scripting interview questions

  1. What does the shebang line do?
  2. Single quotes versus double quotes?
  3. Why should you quote variables like "$file"?
  4. What are exit codes, and how do you check them?
  5. What do set -e, set -u and set -o pipefail do?
  6. Explain stdin, stdout, stderr and redirection such as 2>&1.
  7. How do pipes work?
  8. How do you loop over files safely, including names with spaces?
  9. What are $1, $@, $# and $??
  10. How do you parse command-line options?
  11. grep versus sed versus awk — when to use each?
  12. Find the ten largest files in a directory tree.
  13. Count the most frequent IP addresses in an access log.
  14. How do you schedule a script to run regularly?
  15. How do you debug a shell script?

Quoting, variables and exit codes

Safer scripts

Setting or practiceEffect
set -eExit when a command fails (with some exceptions in conditionals)
set -uTreat unset variables as errors
set -o pipefailA pipeline fails if any command in it fails
trap '…' EXITClean up temporary files on exit
mktempCreate temporary files safely
shellcheckStatic 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

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