3 ms·
The first example ("idiom") is bit tricky, because the title is "Creating an empty file". An alternative would for example be echo -n > example.txt Both f
by zokier 7y ago
The first example ("idiom") is bit tricky, because the title is "Creating an empty file". An alternative would for example be
echo -n > example.txt
Both fit the description of "Creating an empty file". Is it idempotent, or more or less so than `touch example.txt`? If there is already an existing file, then touching it obviously will not empty it, so the end state is not "empty file exists" like one would expect from operation "Creating an empty file"
Ironically article calls "This is an easy one"
- dylan604 7y agoIf a file has a line ending in it, is it truly empty?
- fwip 7y agoThe -n flag keeps echo from emitting a line ending.
- dylan604 7y agodoh! I read it as \n, but then that's for other languages, so i was just all sorts of not paying attention
- mappu 7y agoI used a distro in recent memory where `echo -n foo` really produced `-n foo`. The workaround was `printf "%s" foo`.
- fwip 7y agoAh yeah, posix doesn't require -n to do anything in particular. For most use cases, strict adherence to posix compatibility isn't important, but sometimes it really really is.
- gabrielblack 7y ago"echo -n " is useless, is sufficient: > example.txt without prepending the echo command. This will create an empty file or it will truncate an existent file. The touch command could be preferable to preserve the content, if needed.
- gabrielblack 7y agoP.S. >> example.txt this one (with a double > ) could replace touch in this application case: it's less readable but more efficient.