Which of the following commands will output “onetwothree” ?

2014

Which of the following commands will output “onetwothree” ?

  1. A.

    for val; do echo-n $val; done < one two three

  2. B.

    for one two three; do echo-n-; done

  3. C.

    for n in one two three; do echo-n $n; done

  4. D.

    for n in one two three {echo –n $ n}

Attempted by 407 students.

Show answer & explanation

Correct answer: C

Correct command: for n in one two three; do echo -n $n; done

Why this works:

  • The for loop uses the form: for variable in list; do ...; done — here the variable n takes the values one, two, three in turn.

  • echo -n suppresses the trailing newline. It must be written with a normal hyphen and a space: echo -n

  • Use variable expansion without spaces: $n (not "$ n").

  • Putting these together prints the three words back-to-back, producing: onetwothree

Example output: onetwothree

Explore the full course: Mppsc Assistant Professor