Linux Administration
medium

Pregunta de Linux Administration #019e3987

Enunciado de la Pregunta
Linux Administration
#tee
#echo
#cat
#pipe
Medio

When given the following command line.

echo "foo bar" | tee bar | cat

Which of the following output is created?

A

cat

B

foo bar

Correcto
C

tee bar

D

bar

E

foo

Explicación Oficial / Explicaciones de la Comunidad

Summary

The command echo "foo bar" | tee bar | cat demonstrates the use of pipes and the tee command in Linux. The tee command reads from standard input and writes to both standard output and one or more files. Here, tee bar writes the input to the file bar and also passes it to stdout, which is then piped to cat. cat simply outputs its input to stdout. The final output displayed on the terminal is "foo bar". The question tests understanding of how tee duplicates output and how pipes connect commands.

How it Works

The shell creates a pipeline of three processes: echo, tee, and cat. The pipe (|) connects stdout of one command to stdin of the next. echo "foo bar" writes "foo bar
" to its stdout. This is piped to tee bar. tee opens the file bar for writing, reads from stdin, writes the data to the file, and also writes the same data to its stdout. That stdout is piped to cat. cat reads from stdin and writes to its stdout (the terminal). The kernel handles the data flow through pipe buffers, and tee uses the write() system call to both the file and the pipe.

Why the correct answer is right

The correct answer is B. foo bar. The pipeline produces exactly that output. echo outputs "foo bar", tee passes it through, and cat outputs it unchanged. The file bar is created with the same content, but the question asks about the output created by the command line, meaning what appears on the terminal.

Why incorrect options are wrong

  • A. cat: This is the command name, not output. The command cat does not produce the string "cat" as output.
  • C. tee bar: This is the command and argument, not output. tee does not output its own name.
  • D. bar: This is the filename argument to tee. The output is the content "foo bar", not the filename.
  • E. foo: Only part of the input. The full string "foo bar" is output, not just "foo".