Deal of The Day! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

LPI 010-160 Exam - Topic 5 Question 71 Discussion

Posted by Freeman on Aug 14, 2026 at 04:44 AM

Which of the following examples shows the general structure of a for loop in a shell script?

Suggested Answer: C

The general structure of a for loop in a shell script is as follows12:

for variable in list do commands done

The variable is the name of a loop counter or iterator that takes on the values of the items in the list. The list can be a sequence of words, numbers, filenames, or the output of a command. The commands are the body of the loop that are executed for each value of the variable. The do and done keywords mark the beginning and the end of the loop body.

The option C. for file in *.txt do echo $i done follows this structure, with the variable being file, the list being *.txt (which matches all the files with the .txt extension in the current directory), and the command being echo $i (which prints the value of the variable i, which is presumably set somewhere else in the script).

The other options are incorrect because:

A . for *.txt as file => echo $file uses an invalid syntax for a for loop. The as keyword is not part of the shell script syntax, and the => symbol is not a valid operator. The correct way to write this loop would be:

for file in *.txt do echo $file done

B . for *.txt ( echo $i ) uses an invalid syntax for a for loop. The parentheses are not part of the shell script syntax, and the loop body is missing the do and done keywords. The correct way to write this loop would be:

for i in *.txt do echo $i done

D . for ls *.txt exec {} ; uses an invalid syntax for a for loop. The ls command is not a valid variable name, and the exec {} ; is not a valid command. This looks like a mix of a for loop and a find command. The correct way to write this loop would be:

for file in *.txt do exec $file done

E . foreach @{file} { echo $i } uses an invalid syntax for a for loop. The foreach keyword is not part of the shell script syntax, and the @{file} and { echo $i } are not valid expressions. This looks like a mix of a for loop and a Perl syntax. The correct way to write this loop would be:

for file in * do echo $file done


Looping Statements | Shell Script - GeeksforGeeks

How do I write a 'for' loop in Bash? - Stack Overflow

Get Premium 010-160 Questions as Interactive Web-Based Practice Test or PDF Real Exam Format Detailed Explanations Instant Access Get Your Practice Test
18 comments · newest first
JE
Jettie 2 days ago
Option A seems off. It doesn't follow the loop structure.
BR
Brock 7 days ago
Agreed! C is the only one that uses "in" properly.
VE
Veronika 12 days ago
I think option C looks right. It has the correct syntax.
SH
Sherly 17 days ago
I agree with C. It follows the standard format.
CA
Carla 23 days ago
C is definitely the best choice. Clear and concise.
CH
Chana 28 days ago
D is confusing. Not a typical for loop.
CA
Catalina 1 month ago
A is tempting, but it’s not structured well.
AN
Andrew 1 month ago
C makes sense. It has the correct syntax.
LO
Lore 1 month ago
I think it's C. It looks right.
CA
Cassie 2 months ago
I’m surprised E is even an option, it looks like a different language!
RE
Rebecka 2 months ago
Wait, is D even a for loop? Seems off.
EL
Elly 2 months ago
Definitely not B, that’s just wrong.
DO
Dortha 2 months ago
I thought it was A at first, but C makes more sense.
BE
Beckie 2 months ago
Option C is the correct syntax for a for loop.
BU
Buddy 2 months ago
Option A seems off to me; I don't think you can use "as" in a for loop in shell scripting.
KA
Kaycee 3 months ago
I feel like option C looks the closest to what we practiced, but I’m not entirely sure about the variable name used.
LO
Louvenia 3 months ago
I remember that a for loop usually has a specific structure, but I can't recall if "do" and "done" are required in shell scripts.
LE
Lea 3 months ago
I think I've seen something like this in practice questions, but I'm not sure if the syntax is exactly right.
0/2000 characters

Save Cancel