Date: Sun, 08 Mar 2026 15:54:06 -0600
From: Stan Marsh <[email protected]>
Message-ID: <[email protected]>
| Anyway, I've never had a problem with this, though I certainly assume
| that nothing is guaranteed. It has always seemed likely to me that
| bash *does* do the right thing here - which would be to read the whole
| file into memory at startup and then go from there
Be rational. How do you do that when the "file" is a terminal, and
the source is you typing, and what you are to type isn't decided until
you see the output from previous typing ?
There's no difference with a script, the shell reads as much as it needs
to execute a command (sometimes just a line) and executes that, then goes
back and read more - and if the script is being read from stdin, that is
actually required, as the following input might be for the command being
run, rather than for the shell, the shell just continues reading from
wherever stdin is left positioned after the commands finish.
Things could be slightly different if the script is a file, and will be
passed to the shell as a filename, rather than as stdin, but in that case
how do you guarantee that the whole file will fit in memory? Scripts can
be very large, in the case of 32 bit processors, larger than the available
memory for a process (and the shell needs space for its code, and for
all of its data).
What seems to be being expected to happen (by some) simply cannot happen.
If you want to guarantee that you can modify a script file in place, and
modify it, write the whole script as a function body (say called "main"
but the actual name doesn't matter) with absolutely nothing outside of that
function, except, other function definitions if desired - or those can be in
main - you can convert a script to this form just by adding
main() {
at the start, and then
}
main "$@"; exit
at the end, right at the end, make the last line of the file (not even
a blank line following) be the "main "$@"; exit" line.
Then you can generally modify the script as much as you like, including
using editors that overwrite the file rather than making a new one (better
really, in case the file is linked to other names). You just need to wait
long enough for the shell to have read the whole file, compiled the function,
and started exexuting - at that point it has read everything it will ever
read from the script, if the new main function returns (by reaching its end
normally) then the "exit" will run, and you know what happens then!
kre