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?
forloop), so the change to the shell script's file had no effect.. myscriptwill first parsemyscriptas 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.