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.
More questions on this objective
- A user logs in at a text console, and bash starts as an interactive login shell. Which file in that user's home directory is read by bash itself as part of login startup? machine-checked
- You are already logged in to a graphical desktop and open a new terminal window, so bash starts as an interactive shell that is not a login shell. Which per-user file does bash read in that case? machine-checked
- An executable script contains only the line `export EDITOR=vim`. After running it as `./setenv.sh`, `echo $EDITOR` in the calling shell prints nothing. What explains this? machine-checked
- In bash, what is the effect of `export MYVAR`? machine-checked
- At a bash prompt you run `alias ll='ls -l'` and it works for the rest of that session, but a terminal window opened afterwards does not recognise `ll`. Which statement explains this and gives the standard remedy? machine-checked
- A command finishes and the shell reports an exit status of 0. What does that mean, and which parameter reports it? machine-checked