EX200 Create simple shell scripts Practice Question
A developer wants to create a script that accepts a directory path as an argument and creates a timestamped backup of that directory. If no argument is provided, it should back up the current directory. How should the script handle the argument?
⚠ Common exam trap
Red Hat often tests the distinction between positional parameters (`$1`, `$2`, etc.) and special variables (`$@`, `$0`, `$?`), and the trap here is that candidates confuse `$1` with `$0` (the script name) or incorrectly assume `$@` works as a single default value, leading to option B or C.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
dir=${1:-.}
`${1:-.}` uses the default value substitution syntax in bash: if parameter `$1` (the first positional argument) is unset or null, it expands to `.` (the current directory). This ensures the script backs up the supplied directory path or defaults to the current directory when no argument is provided, exactly matching the requirement.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
dir=${1:-.}
Why this is correct
The parameter expansion `${1:-.}` explicitly targets the first positional parameter (`$1`) and applies the `:-` operator: if `$1` is unset or null, the expansion substitutes the literal `.` (the current directory). This precisely fulfills the requirement to accept a directory argument with a sensible default when no argument is supplied, because `$1` is the first argument passed to the script, and the default value is only used when that argument is missing or empty.
- ✗
dir=${@:-.}
Why it's wrong here
The expression `${@:-.}` is conceptually flawed because `$@` represents all positional parameters as a list, not the first argument. The `:-` operator, when applied to `$@` in this form, does not isolate the initial directory name; instead, it expands to the entire argument list, or to `.` only if no arguments exist at all. This would assign the full list of arguments (or current directory) to `dir`, which breaks the intended single-directory semantics and can produce unexpected word-splitting or quoting behavior.
- ✗
dir=${0:-.}
Why it's wrong here
The use of `${0:-.}` is categorically wrong because `$0` is the name of the script itself, not the first positional argument. The `:-` operator is essentially a no-op here because `$0` is virtually always set by the shell when the script runs; even if it were unset, the expansion would substitute `.` only in that extremely unlikely scenario, leaving `dir` set to the script's filename, which is not a directory path and cannot satisfy the requirement.
- ✗
dir=${?:-.}
Why it's wrong here
The expansion `${?:-.}` is invalid as a directory argument because `$?` holds the exit status of the most recently executed foreground pipeline, not an argument or path. The `:-` combination produces a value like `.-` when the status is non-null (which it always is after any command), or `.` if the status is null, but neither result is a directory path. Using `$?` here is a misinterpretation of shell parameters, as the special variable `$?` has no connection to command-line arguments.
Visual reference
Go deeper
Related to this question
About these practice questions
One of 127 original EX200 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This EX200 practice question is part of Courseiva's free Red Hat certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the EX200 exam.