Which of the following commands will output “onetwothree” ?
2014
Which of the following commands will output “onetwothree” ?
- A.
for val; do echo-n $val; done < one two three
- B.
for one two three; do echo-n-; done
- C.
for n in one two three; do echo-n $n; done
- 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