4 ms·
I'm quite glad it is a thing - not sure whether this is the reason, but I quite like creating small files using the `cat` command: cat - > some_filename.tx
by undecisive 6y ago
I'm quite glad it is a thing - not sure whether this is the reason, but I quite like creating small files using the `cat` command:
cat - > some_filename.txt
To finish the file, you have to add a new line - then, at the start of that line, press Ctrl-D
So with tools where you have to insert an EOF manually like that, it's impossible to have a file that ends without a newline.
- jolmg 6y agoNot exactly. `cat` stops reading when read(2) returns an empty string (read(2) doing that is what's interpreted as EOF). Ctrl-D doesn't insert anything. Rather it flushes the terminal's output buffer (the one that's filled with user input) into the underlying program's input. The terminal does line buffering by default to allow the user to modify the line before submitting it (e.g. deleting with backspace or Ctrl-W). That means that it automatically flushes when reaching the end of line (i.e. the newline). If you flush again with Ctrl-D at the start of a line, that means cat's call to read(2) will return an empty string, since you're flushing an empty buffer. That means that: > So with tools where you have to insert an EOF manually like that, it's impossible to have a file that ends without a newline. is false. It's not impossible, just impractical. You can have a file with an incomplete line at the end by starting a line and hitting Ctrl-D twice. Once to submit the incomplete line, and then again to flush the empty buffer, thereby indicating EOF.
- undecisive 6y agoWell I never... very interesting. Thanks!