010-100 · Question #23
Which of the following command sequences overwrites the file foobar.txt?
The correct answer is C. echo "QUIDQUIDAGIS" > foobar.txt. The > redirection operator overwrites the destination file with the command output, while >> appends to it.
Question
Which of the following command sequences overwrites the file foobar.txt?
Options
- Aecho "QUIDQUIDAGIS" >> foobar.txt
- Becho "QUIDQUIDAGIS" < foobar.txt
- Cecho "QUIDQUIDAGIS" > foobar.txt
- Decho "QUIDQUIDAGIS" | foobar.txt
How the community answered
(24 responses)- A8% (2)
- C79% (19)
- D13% (3)
Why each option
The > redirection operator overwrites the destination file with the command output, while >> appends to it.
The >> operator appends output to the end of an existing file rather than overwriting it.
The < operator redirects a file as standard input to a command; it does not write output to foobar.txt.
The > operator redirects standard output to a file and truncates (overwrites) any existing content in that file before writing. This causes foobar.txt to contain only the new string QUIDQUIDAGIS after execution.
The | operator pipes output from one command to another command's standard input; foobar.txt is a file, not a command, so this would result in an error.
Concept tested: Shell output redirection overwrite vs append
Source: https://www.gnu.org/software/bash/manual/bash.html#Redirections
Topics
Community Discussion
5C is correct. The single greater-than sign redirects standard output to the file and truncates it first, so whatever was in foobar.txt before is gone and replaced with QUIDQUIDAGIS. The double greater-than in A appends instead of overwrites, B tries to redirect the file as input into echo which does not make sense, and D pipes to a filename which is not a valid command.
Good breakdown, and one thing worth adding for the exam is that the single redirect will also create foobar.txt from scratch if it does not already exist, so it acts as both a create and an overwrite depending on whether the file was there before.
So if I'm reading this right, the single greater-than sign is what actually replaces whatever was already in the file, while two of them just adds to the end, so wouldn't that make C the one that overwrites? And just to make sure I have this straight, does the direction the arrow points matter here, or is it purely about how many you use?
I almost circled A because those double arrows looked way more aggressive, like they would bulldoze the whole file, but then my little hook kicked in, ONE arrow is a funnel that flushes the file empty and pours fresh content in, TWO arrows just tacks new text onto the end like a sticky note on top of what is already there. So lock it in with this image, a single greater-than sign is a drain that empties the tub before refilling it, and C is your overwriter every single time.
Good image but flip the labels, single arrow is the one that overwrites and double arrow is the append, so C is right but call it the drain that empties first, not a sticky note.