Linux and Command-Line Basics
$ echo a > f.txt
$ echo b > f.txt
$ echo c >> f.txt
$ cat f.txtWhen the commands above are run in order, which of the following is the content shown by the last cat?
- AOnly the one line c
- BThe two lines b and c
- CThe three lines a, b and c
Show answer and explanation
Answer: B. The two lines b and c
">" is a redirection that writes output to a file; if the file already exists, it is emptied before writing (overwritten). ">>" appends to the end of the file. The first line writes a, the ">" in the second line leaves only b, and the ">>" in the third line adds c after it, so f.txt contains the two lines b and c. To avoid wiping out an important file with >, bash can forbid overwriting with set -o noclobber.
- A. ">>" appends rather than overwrites, so the b written in the second line remains.
- B. Correct. > leaves only b, and >> appends c.
- C. The ">" in the second line empties the file before writing, so the a from the first line does not remain.