I had always thought that shells parse whole scripts, constructing an AST, and then execute that AST from memory. However, I just read a comment by Stéphane Chazelas, and tested executing this script, edit-while-executing.sh:
#!/bin/bash
echo start
sleep 10
and then while it was sleeping running:
$ echo "echo end" >> edit-while-executing.sh
and it worked to cause it to print "end" at the end.
However, when trying to modify this:
#!/bin/bash
while true; do
echo yes
done
by doing:
$ printf "%s" "no " | dd of=edit-while-executing.sh conv=notrunc seek=35 bs=1
It didn't work, and kept printing "yes".
I also wondered if other non-shell interpreters also worked like this, and tried the equivalent of the first script with python, but it didn't work. Though, maybe python is not an interpreter anymore and it's more of a JIT compiler.
So to reiterate my question, is this a behaviour ubiquitous to shells and limited to them or also present in other interpreters (those not regarded as shells)? Also how does this work such that could I do the first modification but not the second?
for
loop), so the change to the shell script's file had no effect. – Kusalananda Jun 13 '18 at 16:04. myscript
will first parsemyscript
as one compound statement and then execute it. This is e.g. a reason why aliases defined in such a script are not active inside the script already. – schily Jun 13 '18 at 16:11alias
documentation, it is even commonly accepted. – schily Jun 13 '18 at 16:15eval
and commands that are executed for command substitution. You may verify this by checking for theNLFLG
as parameter to the parser that tells the parser to treat newlines as semicolons. – schily Jun 13 '18 at 16:21\<newline>
processing occur first in every parsing case, and soeval
- which must reparse - must correctly handle backslash escaped newlines. if you amd your peers are devolving that capability in the interest of compiled in hacks and special sauce, stop. – mikeserv Jan 22 '19 at 02:54