A script takes an optional directory as its first argument and must fall back to /var/log when it is called with no arguments at all. The positional parameters themselves must not be changed. Which line does that?

LPIC-1 Exam 102-500, objective 105. Shells and shell scripting medium

Machine-checked — no person has signed for it. This question was read against the source cited below by an automated pass, which found no contradiction. That is a weaker claim than it sounds: the same kind of process wrote the question, so it can confirm its own mistake.

Treat it as a good draft rather than as settled fact, and read the source below before you rely on it. It is not used in mock exams here — only questions a person has signed for are.

How these questions are written — where each question comes from, what the verification ledger records, and what happens when one is found wrong.

The options

Correct TARGET="${1:-/var/log}"

Correct. The :- form substitutes the word /var/log when $1 is unset or empty, and leaves $1 itself untouched. The value ends up only in TARGET, which is what the requirement asks for.

Not correct TARGET="${1:=/var/log}"

Wrong. The := form assigns the default back into the parameter, and bash refuses to assign to a positional parameter: it reports that $1 cannot be assigned in this way and the expansion fails. This form is usable only with ordinary variables.

Not correct TARGET="${1:?/var/log}"

Wrong. The :? form treats the word as an error message rather than a value. With no first argument it prints that message to standard error and a non-interactive shell exits, so the script stops instead of defaulting.

Not correct TARGET="${1:+/var/log}"

Wrong, and exactly inverted. The :+ form substitutes /var/log when $1 IS set and non-empty, and substitutes nothing when it is unset, so calling the script with no arguments leaves TARGET empty.

Why

Four parameter expansions share one shape and differ in what the word after the operator is for: :- uses it as a default value, := uses it as a default and assigns it back, :? uses it as an error message, and :+ uses it as an alternate value when the parameter is set. Omitting the colon in any of them narrows the test from unset-or-empty to unset only, so ${1-/var/log} keeps an argument that was deliberately passed as an empty string while ${1:-/var/log} replaces it. Quoting the whole expansion matters as much as choosing the right form, since an unquoted default containing spaces would be split into several words.

Where this comes from

Cited
manual page bash(1)

Practise this

Reading one question is not practice. The trainer will draw a short set from objective 105 and space the ones you get wrong.

Practise LPIC-1 Exam 102-500

More questions on this objective

All questions on Shells and shell scripting

Practise LPIC-1 Exam 102-500