Linux Administration
medium

Questão de Linux Administration #019e3987

Enunciado da Questão
Linux Administration
#for-loop
#echo
#variable-expansion
#token
Médio

What is the output of the following command?

for token in a b c; do

echo -n ${token};

done

A

anbncn

B

abc

Correta
C

token$token

D

{a}{b}{c}

E

a b c

Explicação Oficial / Explicações da Comunidade

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 for loop syntax for token in a b c; do ... done assigns each word after in to the variable token sequentially.
  • ${token} expands to the value of the variable token.
  • echo -n prints the string without appending a newline character.
  • Therefore, the first iteration prints a, the second prints b immediately after, and the third prints c, resulting in abc.

Why Other Options Are Incorrect

  • anbncn: This would require appending n after each token, which is not present in the command.
  • $token$token$token: This would be the output if the variable token were 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 if echo -n were replaced with echo (which adds spaces between arguments) or if the tokens were printed with spaces. The command uses -n and prints each token separately without spaces.