010-100 · Question #22
What is the output of the following command? for token in a b c; do echo -n ${token}; done
The correct answer is B. abc. The for loop iterates over tokens a, b, and c, and echo -n prints each without a trailing newline, producing concatenated output abc.
Question
What is the output of the following command? for token in a b c; do echo -n ${token}; done
Options
- Aanbncn
- Babc
- C$token$token$token
- D{a}{b}{c}
- Ea b c
How the community answered
(26 responses)- A12% (3)
- B81% (21)
- D4% (1)
- E4% (1)
Why each option
The for loop iterates over tokens a, b, and c, and echo -n prints each without a trailing newline, producing concatenated output abc.
anbncn would imply literal 'n' characters are printed, but -n is a flag to echo that suppresses newlines, not a character to output.
The for loop assigns each value (a, b, c) to the variable token in sequence. The echo -n flag suppresses the newline after each output, so all three values are printed consecutively on one line, resulting in abc.
$token$token$token would be the output only if variable expansion were disabled (e.g., inside single quotes), but the shell expands $token to its current value.
{a}{b}{c} is not valid shell output; curly braces have no special meaning in this context and the variable $token is expanded normally.
a b c with spaces would be the output if echo were called once with all tokens as arguments or if newlines separated them, but -n and sequential echo calls produce no spaces.
Concept tested: Bash for loop and echo -n output behavior
Source: https://www.gnu.org/software/bash/manual/bash.html#Looping-Constructs
Topics
Community Discussion
No community discussion yet for this question.