zsh-users
 help / color / mirror / code / Atom feed
* echo "true" > ~/.zshrc from ~/.zshrc
@ 2016-06-11 16:23 Sebastian Gniazdowski
  2016-06-11 16:34 ` Nikolay Aleksandrovich Pavlov (ZyX)
  2016-06-11 18:05 ` Bart Schaefer
  0 siblings, 2 replies; 4+ messages in thread
From: Sebastian Gniazdowski @ 2016-06-11 16:23 UTC (permalink / raw)
  To: Zsh Users

Hello,
How legal it is to do:

echo "true" > ~/.zshrc

>From ~/.zshrc? What can be expected?

When I do (in ~/.zshrc, at last line):

echo "sleep 200" >> ~/.zshrc

then I'll get the sleep at next startup. When I do:

echo "true" > ~/.zshrc
echo "Hello normally ended"

Then I will see the "Hello" message.

But when I output much of text earlier in 400 lines long zshrc, then e.g.:

/Users/sgniazdowski/.zshrc:182: command not found: fas

But other time it went without errors, and without "Hello" message.

Best regards,
Sebastian Gniazdowski


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: echo "true" > ~/.zshrc from ~/.zshrc
  2016-06-11 16:23 echo "true" > ~/.zshrc from ~/.zshrc Sebastian Gniazdowski
@ 2016-06-11 16:34 ` Nikolay Aleksandrovich Pavlov (ZyX)
  2016-06-11 17:40   ` Sebastian Gniazdowski
  2016-06-11 18:05 ` Bart Schaefer
  1 sibling, 1 reply; 4+ messages in thread
From: Nikolay Aleksandrovich Pavlov (ZyX) @ 2016-06-11 16:34 UTC (permalink / raw)
  To: Sebastian Gniazdowski, Zsh Users

11.06.2016, 19:24, "Sebastian Gniazdowski" <sgniazdowski@gmail.com>:
> Hello,
> How legal it is to do:
>
> echo "true" > ~/.zshrc
>
> From ~/.zshrc? What can be expected?
>
> When I do (in ~/.zshrc, at last line):
>
> echo "sleep 200" >> ~/.zshrc
>
> then I'll get the sleep at next startup. When I do:
>
> echo "true" > ~/.zshrc
> echo "Hello normally ended"
>
> Then I will see the "Hello" message.
>
> But when I output much of text earlier in 400 lines long zshrc, then e.g.:
>
> /Users/sgniazdowski/.zshrc:182: command not found: fas
>
> But other time it went without errors, and without "Hello" message.
>
> Best regards,
> Sebastian Gniazdowski

What do you mean by “legal”? If you do something like this you need to understand what is going on: see `man zshmisc`, section “REDIRECTION”. Specifically `>>` appends to zshrc, `>` *empties* zshrc and then appends: nearly equivalent is `rm ~/.zshrc; echo true > ~/.zshrc`.

I personally would not suggest to *ever* edit zshrc using `echo … >[>] ~/.zshrc`: this is a script and using `echo >>` to edit it will over time turn zshrc into an unorganized junkyard. If you need to do some edits, take text editor and edit ~/.zshrc, keeping it organized. This is especially needed if you have many aliases which may alter the subsequent code.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: echo "true" > ~/.zshrc from ~/.zshrc
  2016-06-11 16:34 ` Nikolay Aleksandrovich Pavlov (ZyX)
@ 2016-06-11 17:40   ` Sebastian Gniazdowski
  0 siblings, 0 replies; 4+ messages in thread
From: Sebastian Gniazdowski @ 2016-06-11 17:40 UTC (permalink / raw)
  To: Nikolay Aleksandrovich Pavlov (ZyX); +Cc: Zsh Users

On 11 June 2016 at 18:34, Nikolay Aleksandrovich Pavlov (ZyX)
<kp-pav@yandex.ru> wrote:
> What do you mean by “legal”?

This seems like absurd thing to do, I was searching for some
reasonable constraints in this, thus the word "legal".

> If you do something like this you need to understand what is going on: see `man zshmisc`, section “REDIRECTION”. Specifically `>>` appends to zshrc, `>` *empties* zshrc and then appends: nearly equivalent is `rm ~/.zshrc; echo true > ~/.zshrc`.

Thanks

> I personally would not suggest to *ever* edit zshrc using `echo … >[>] ~/.zshrc`: this is a script and using `echo >>` to edit it will over time turn zshrc into an unorganized junkyard. If you need to do some edits, take text editor and edit ~/.zshrc, keeping it organized. This is especially needed if you have many aliases which may alter the subsequent code.

Thanks for the point about aliases

-- 
Best regards,
Sebastian Gniazdowski


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: echo "true" > ~/.zshrc from ~/.zshrc
  2016-06-11 16:23 echo "true" > ~/.zshrc from ~/.zshrc Sebastian Gniazdowski
  2016-06-11 16:34 ` Nikolay Aleksandrovich Pavlov (ZyX)
@ 2016-06-11 18:05 ` Bart Schaefer
  1 sibling, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2016-06-11 18:05 UTC (permalink / raw)
  To: Zsh Users

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.


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2016-06-11 18:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-06-11 16:23 echo "true" > ~/.zshrc from ~/.zshrc Sebastian Gniazdowski
2016-06-11 16:34 ` Nikolay Aleksandrovich Pavlov (ZyX)
2016-06-11 17:40   ` Sebastian Gniazdowski
2016-06-11 18:05 ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).