Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: echo "true" > ~/.zshrc from ~/.zshrc



On Jun 11,  6:23pm, Sebastian Gniazdowski wrote:
}
} How legal it is to do:
} 
} echo "true" > ~/.zshrc
} 
} From ~/.zshrc? What can be expected?

See my earlier mention of how scripts are executed line by line as they
are read.  If you truncate ~/.zshrc this way, then the only parts of it
that will be executed are the parts that were already slurped into the
operating system I/O buffer before the file was truncated.

If you truncate the file and then write more bytes to it than had fit
in the I/O buffer (or more than were in the file in the first place),
then when the buffer is next filled (at some indeterminate future read
by the zsh parser) you're likely to get just part of what you wrote,
i.e., the tail after skipping the number of bytes that were previously
buffered.

Or you might just get an I/O error of some kind, it depends on the OS.

Further, if you truncate and write FEWER bytes than previously read,
you may end up with a file that has a whole bunch of NUL ('\0') butes
at the end to pad it out to the size it had before you tried to write
it while it was still being read.  This used to happen a lot with NFS
filesystems.

This isn't zsh specific, this can happen with any file that is written
while another process is reading it.  It's a bit more likely to do odd
things if the SAME process is doing both the reading and the writing,
though, which is the situation you've created.

If you want to do this kind of thing, write to a new file and then
rename it to ~/.zshrc to avoid opening the same file twice.



Messages sorted by: Reverse Date, Date, Thread, Author