In Bash, what does the loop for i in "${animals[@]}"; do echo $i; done print?

Study for the OSCP Linux Exam. Use our flashcards and multiple-choice questions to test your skills. Each query comes with detailed hints and explanations to enhance your preparedness. Get ready to conquer the exam!

Multiple Choice

In Bash, what does the loop for i in "${animals[@]}"; do echo $i; done print?

Explanation:
The loop relies on array expansion that feeds the loop with each element as a separate item. Using "${animals[@]}" means Bash delivers every element of the array in order, one by one, to the loop. Each pass assigns the current element to i, and echo $i prints that element. Since the elements are processed in their original order, you see all elements printed, one per line, in the array’s order. If an element contains spaces, this form still treats it as a single element, and echo "$i" would print it exactly as stored.

The loop relies on array expansion that feeds the loop with each element as a separate item. Using "${animals[@]}" means Bash delivers every element of the array in order, one by one, to the loop. Each pass assigns the current element to i, and echo $i prints that element. Since the elements are processed in their original order, you see all elements printed, one per line, in the array’s order. If an element contains spaces, this form still treats it as a single element, and echo "$i" would print it exactly as stored.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy