nerdexam
LPI

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.

The Power of the Command Line

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)
  • A
    12% (3)
  • B
    81% (21)
  • D
    4% (1)
  • E
    4% (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.

Aanbncn

anbncn would imply literal 'n' characters are printed, but -n is a flag to echo that suppresses newlines, not a character to output.

BabcCorrect

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.

C$token$token$token

$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.

D{a}{b}{c}

{a}{b}{c} is not valid shell output; curly braces have no special meaning in this context and the variable $token is expanded normally.

Ea b c

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

#for loop#variable expansion#shell scripting#bash syntax

Community Discussion

No community discussion yet for this question.

Full 010-100 Practice