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.

Practise LPIC-1 Exam 102-500

More questions on this objective

All questions on Shells and shell scripting

Practise LPIC-1 Exam 102-500