1. On Ubuntu, what does /bin/sh usually point to?
- dash
- bash
- zsh
- fish
Show answer
Answer: dash
Debian and Ubuntu use dash as /bin/sh for speed and strict POSIX behaviour.
Lesson: What Bash Is
25 questions from the Bash / Shell Scripting course, each with the correct answer and an explanation. Try to answer before you open the answer.
Answer: dash
Debian and Ubuntu use dash as /bin/sh for speed and strict POSIX behaviour.
Lesson: What Bash Is
Answer: The command succeeded
By convention, 0 means success and non-zero means some kind of failure.
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.
Answer: count=3
Bash assignments must not have spaces around the equals sign.
Lesson: Variables and the Environment
Answer: rm receives two arguments, "my" and "file.txt"
Unquoted expansion undergoes word splitting on the space.
Answer: report.tar
% removes the shortest matching suffix, so only ".gz" is removed.
Lesson: Parameter Expansion, Command Substitution and Arithmetic
Answer: (( n > 10 ))
Arithmetic evaluation compares numbers; > inside [[ ]] compares strings and inside [ ] is a redirection.
Lesson: Tests: [[ ]], [ ] and (( ))
Answer: Only if cmd1 fails
|| runs the right-hand command only when the left-hand one returns a non-zero status.
Answer: It preserves spaces and backslashes exactly as they appear in each line
Empty IFS keeps surrounding whitespace and -r stops backslash interpretation.
Answer: Print it and let the caller capture it with $(function)
return only sets a numeric status; data is returned by printing to stdout.
Answer: "${arr[@]}"
"${arr[@]}" expands each element as its own word, preserving spaces inside elements.
Lesson: Indexed and Associative Arrays
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
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
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.
Answer: jq
jq parses JSON structurally; text tools break when formatting changes.
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.
Answer: trap cleanup EXIT
The EXIT pseudo-signal fires whenever the shell exits, for any reason it can handle.
Answer: Prints each command after expansion before running it
Tracing shows the exact expanded commands, which is invaluable for debugging.
Answer: SIGKILL
SIGKILL (9) terminates immediately; it cannot be trapped, which is why it is a last resort.
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
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.
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
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.
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.
Answer: other_cmd "$@"
"$@" expands each argument as its own word, preserving spaces inside arguments.
Lesson: Revision and Interview Questions