Linux Administration
medium
Pregunta de Linux Administration #019e3987
Enunciado de la Pregunta
Linux Administration
#for-loop
#echo
#variable-expansion
#token
Medio
What is the output of the following command?
for token in a b c; do
echo -n ${token};
done
A
anbncn
B
abc
Correcto
C
tokentoken$token
D
{a}{b}{c}
E
a b c
Explicación Oficial / Explicaciones de la Comunidad
The command iterates over the list a b c using a for loop. In each iteration, the variable token takes the value of the current item. The echo -n ${token} command prints the value of token without a trailing newline (due to -n). Thus, the output is the concatenation of the three tokens: abc.
How it Works
- The
forloop syntaxfor token in a b c; do ... doneassigns each word afterinto the variabletokensequentially. ${token}expands to the value of the variabletoken.echo -nprints the string without appending a newline character.- Therefore, the first iteration prints
a, the second printsbimmediately after, and the third printsc, resulting inabc.
Why Other Options Are Incorrect
anbncn: This would require appendingnafter each token, which is not present in the command.$token$token$token: This would be the output if the variabletokenwere not expanded (e.g., if single quotes were used). However,${token}is expanded.{a}{b}{c}: This would require brace expansion or explicit braces in the echo command, which are not present.a b c: This would be the output ifecho -nwere replaced withecho(which adds spaces between arguments) or if the tokens were printed with spaces. The command uses-nand prints each token separately without spaces.