Which three loop headers cause a bash script to iterate exactly over the numbers 1 to 5? Assume default bash options and an empty current directory. (Choose three.)
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
Choose 3.
Not correct for i in 1 to 5; do
Wrong. There is no `to` keyword in bash. This loops three times, over the literal words 1, to and 5.
Correct for i in {1..5}; do
Correct. Brace expansion produces the words 1 2 3 4 5. Note that it happens before variable expansion, so {1..$n} does not work.
Correct for i in $(seq 1 5); do
Correct. Command substitution runs seq, which prints 1 to 5 one per line, and the shell splits that output into words. Unlike brace expansion this accepts variables.
Correct for (( i=1; i<=5; i++ )); do
Correct. This is bash's arithmetic for loop, using the same three-part form as C. It is a bash extension, not POSIX sh.
Not correct for i in [1-5]; do
Wrong. Square brackets form a filename glob matching one character in the range. If no file in the current directory is named 1 through 5, the pattern stays unmatched and the loop runs once with i set to the literal string [1-5].
Why
A `for NAME in WORDS` loop iterates over a word list, so the interesting part is how you generate the list: brace expansion {1..5} for literal ranges, $(seq FIRST LAST) when the bounds come from variables, or the arithmetic (( )) form when you want C-style control. Brace expansion is the fastest since no external process runs.
Where this comes from
- Cited
- LPI exam objective 105.2
- What it says
- Use loops and command substitution in shell scripts.
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