Bash / Shell Scripting interview questions

25 questions from the Bash / Shell Scripting course, each with the correct answer and an explanation. Try to answer before you open the answer.

Shells and Your First Script

1. On Ubuntu, what does /bin/sh usually point to?

  1. dash
  2. bash
  3. zsh
  4. fish
Show answer

Answer: dash

Debian and Ubuntu use dash as /bin/sh for speed and strict POSIX behaviour.

Lesson: What Bash Is

2. What does an exit status of 0 mean?

  1. The command failed
  2. The command was killed by a signal
  3. The command was not found
  4. The command succeeded
Show answer

Answer: The command succeeded

By convention, 0 means success and non-zero means some kind of failure.

Lesson: Shebang, Permissions, PATH and Exit Codes

3. Why must cd be a shell builtin rather than an external program?

  1. External programs cannot read directories
  2. cd is too small to be a program
  3. A separate process cannot change the working directory of the parent shell
  4. POSIX forbids external cd
Show answer

Answer: A separate process cannot change the working directory of the parent shell

Each process has its own working directory; only the shell itself can change its own.

Lesson: Builtins, External Commands, Aliases and Functions

Variables, Quoting and Expansion

4. Which assignment is valid Bash?

  1. count = 3
  2. $count=3
  3. count=3
  4. let count := 3
Show answer

Answer: count=3

Bash assignments must not have spaces around the equals sign.

Lesson: Variables and the Environment

5. A variable holds "my file.txt". What happens with `rm $f` (unquoted)?

  1. The file "my file.txt" is removed
  2. rm receives two arguments, "my" and "file.txt"
  3. Bash refuses to run the command
  4. Nothing; quotes are optional
Show answer

Answer: rm receives two arguments, "my" and "file.txt"

Unquoted expansion undergoes word splitting on the space.

Lesson: Quoting, Word Splitting and Globbing

Conditions and Loops

7. Which test correctly checks whether the number in $n is greater than 10?

  1. (( n > 10 ))
  2. [[ $n > 10 ]]
  3. [ $n > 10 ]
  4. [[ $n == >10 ]]
Show answer

Answer: (( n > 10 ))

Arithmetic evaluation compares numbers; > inside [[ ]] compares strings and inside [ ] is a redirection.

Lesson: Tests: [[ ]], [ ] and (( ))

8. In `cmd1 || cmd2`, when does cmd2 run?

  1. Always
  2. Only if cmd1 succeeds
  3. Never
  4. Only if cmd1 fails
Show answer

Answer: Only if cmd1 fails

|| runs the right-hand command only when the left-hand one returns a non-zero status.

Lesson: if, elif, case and Short-Circuit Operators

9. Why is `while IFS= read -r line` preferred for reading lines?

  1. It is faster than every other method
  2. It reads the whole file into memory at once
  3. It preserves spaces and backslashes exactly as they appear in each line
  4. It sorts the lines
Show answer

Answer: It preserves spaces and backslashes exactly as they appear in each line

Empty IFS keeps surrounding whitespace and -r stops backslash interpretation.

Lesson: for, while and Reading Lines Safely

Functions, Arrays and Arguments

10. How should a Bash function return a computed string to its caller?

  1. return "$value"
  2. Set $? to the string
  3. Print it and let the caller capture it with $(function)
  4. exit "$value"
Show answer

Answer: Print it and let the caller capture it with $(function)

return only sets a numeric status; data is returned by printing to stdout.

Lesson: Functions, Arguments and Return Values

11. How do you expand every element of an array as separate, correctly quoted words?

  1. $arr
  2. "${arr[@]}"
  3. ${arr}
  4. ${arr[*]}
Show answer

Answer: "${arr[@]}"

"${arr[@]}" expands each element as its own word, preserving spaces inside elements.

Lesson: Indexed and Associative Arrays

12. In `getopts "vne:h" opt`, what does the colon after e mean?

  1. -e takes a value, which is placed in $OPTARG
  2. -e is optional
  3. -e is a long option
  4. -e must be the last option
Show answer

Answer: -e takes a value, which is placed in $OPTARG

A colon after an option letter means that option requires an argument.

Lesson: Parsing Options with getopts

Input, Output and Text Processing

13. Which command captures both stdout and stderr of build.sh in build.log?

  1. ./build.sh > build.log 2>&1
  2. ./build.sh 2>&1 > build.log
  3. ./build.sh 2> build.log
  4. ./build.sh < build.log
Show answer

Answer: ./build.sh > build.log 2>&1

Redirect stdout to the file first, then point stderr at the same place.

Lesson: Redirection and Here-Documents

14. Without pipefail, what is the exit status of `false | true`?

  1. 1, because false failed
  2. 2
  3. It depends on the shell version only
  4. 0, because the status of the last command is used
Show answer

Answer: 0, because the status of the last command is used

By default a pipeline returns the status of its last command; pipefail changes that.

Lesson: Pipelines, pipefail, Process Substitution and xargs

15. Which tool is best for extracting a field from a JSON API response in a shell script?

  1. grep
  2. cut
  3. jq
  4. wc
Show answer

Answer: jq

jq parses JSON structurally; text tools break when formatting changes.

Lesson: Text Processing with grep, sed, awk and jq

Writing Robust Scripts

16. Why can `local out=$(failing_command)` hide a failure even with set -e?

  1. set -e is disabled inside functions
  2. Command substitution always returns 0
  3. The status of the line is that of the local builtin, which succeeds
  4. local makes variables read-only
Show answer

Answer: The status of the line is that of the local builtin, which succeeds

local returns its own success status, masking the command substitution's failure; declare and assign separately.

Lesson: Strict Mode: set -euo pipefail and Its Caveats

17. Which trap is the most general way to ensure cleanup runs when a script ends?

  1. trap cleanup INT
  2. trap cleanup EXIT
  3. trap cleanup HUP
  4. trap cleanup DEBUG
Show answer

Answer: trap cleanup EXIT

The EXIT pseudo-signal fires whenever the shell exits, for any reason it can handle.

Lesson: Traps, Cleanup and Temporary Files

18. What does `set -x` do?

  1. Prints each command after expansion before running it
  2. Exits on the first error
  3. Makes variables read-only
  4. Disables globbing
Show answer

Answer: Prints each command after expansion before running it

Tracing shows the exact expanded commands, which is invaluable for debugging.

Lesson: Debugging, ShellCheck and Formatting

Processes, Files and Automation

19. Which signal cannot be caught or ignored by a process?

  1. SIGKILL
  2. SIGTERM
  3. SIGINT
  4. SIGHUP
Show answer

Answer: SIGKILL

SIGKILL (9) terminates immediately; it cannot be trapped, which is why it is a last resort.

Lesson: Background Jobs, wait and Signals

20. Why write to a temporary file in the same directory and then mv it over the target?

  1. mv compresses the file
  2. It avoids needing permissions
  3. Temporary files are faster to read
  4. A rename within one file system is atomic, so readers never see a partially written file
Show answer

Answer: A rename within one file system is atomic, so readers never see a partially written file

Atomic rename guarantees readers see either the complete old or complete new file.

Lesson: Safe File Handling

21. Why do scripts often fail under cron even though they work in a terminal?

  1. cron cannot run Bash at all
  2. cron only allows one script per day
  3. cron runs with a minimal environment: short PATH, no profile and a different working directory
  4. cron removes execute permissions
Show answer

Answer: cron runs with a minimal environment: short PATH, no profile and a different working directory

The stripped-down environment breaks scripts that rely on interactive settings.

Lesson: Scheduling with cron and systemd Timers

Script Design, Limits and Revision

22. Why wrap a script's logic in a main function called at the end with main "$@"?

  1. Bash requires a main function
  2. It makes the script run faster
  3. The whole file is parsed before anything runs, and the structure is clearer
  4. It disables set -e
Show answer

Answer: The whole file is parsed before anything runs, and the structure is clearer

Calling main at the bottom ensures every function is defined first and guards against partial execution.

Lesson: A Production Script Template

23. Which situation most strongly suggests rewriting a Bash script in Python or Go?

  1. The script runs three commands in order
  2. The script has grown to hundreds of lines with nested data structures and floating-point maths
  3. The script needs to set an environment variable
  4. The script uses a here-document
Show answer

Answer: The script has grown to hundreds of lines with nested data structures and floating-point maths

Complex data and logic are where Bash becomes error-prone and hard to maintain.

Lesson: When Not to Use Bash, and Portability

24. In the case study, why is pg_dump piped into gzip only safe with pipefail?

  1. Without pipefail, a failed pg_dump would be hidden by gzip's successful exit status
  2. gzip needs pipefail to compress
  3. pipefail speeds up pg_dump
  4. pipefail encrypts the backup
Show answer

Answer: Without pipefail, a failed pg_dump would be hidden by gzip's successful exit status

The pipeline status would otherwise come from gzip, masking a failed dump.

Lesson: Case Study: A Database Backup Script

25. Which line correctly passes all of a script's arguments, unchanged, to another command?

  1. other_cmd $*
  2. other_cmd $@
  3. other_cmd "$*"
  4. other_cmd "$@"
Show answer

Answer: other_cmd "$@"

"$@" expands each argument as its own word, preserving spaces inside arguments.

Lesson: Revision and Interview Questions

← Back to the course