Created
August 21, 2012 22:01
-
-
Save hmason/3419831 to your computer and use it in GitHub Desktop.
Did you know that bash will reload a script *while it is executing*?!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
function addnext { | |
NUM=$1 | |
sleep 1 | |
echo HI $NUM | |
NUM=$(expr $NUM + 1) | |
echo addnext $NUM >> $0 | |
} | |
addnext 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A simple strace shows that it is not reloading anything.
script:
I did not check bash source code but I would imagine it would never read the entire script in memory. It should read it by chunks as it executes it. I am not sure why it does that lseek() but probably just to go to the beginning of the last line and continue reading from there. Just a guess. So, the behaviour of the script "changing" one the fly would depend on how the filesystem react to the concurrent reading and writing.
I may be wrong, this is just my guess based on what I would expect and what I see in strace on Linux....