1

Recently, I have found the article about editing shell script while it's running.

Edit shell script while it's running

I prepared that code to reproduce the phenomenon with additional python script calling. I found echo in bash was NOT affected by editing while python script was affected.

Could someone explain this phenomenon? I have expected that all std outputs should be "modified".

  • test.sh
#!/bin/bash
sleep 30

echo "not modified"
python my_python.py
echo "not modified"
  • my_python.py
print("not modified")
  • Output result
$ bash test.sh  // while sleeping, I edited test.sh and my_python.py to "modified"
not modified
 modified
not modified
jef
  • 3,890
  • 10
  • 42
  • 76

1 Answers1

3

The bash script is already loaded in memory and executing and the results will not be affected until the next run. The python script is not loaded yet and is loaded in memory after you modify it.

If you do the reverse and launch the bash script from an equivalent python script, you will get the same behavior in reverse.

EDIT 05/10/2020

As pointed out by Gordon Davisson;

"Different versions of bash do different things. Some read the file byte-by-byte as they execute it, some I think load it in 8KB blocks (not necessarily the whole file), some do even more complicated things (AIUI it can also depend on the OS they're running under). See my answer Overwrite executing bash script files. Net result: do not count on any particular behavior."

That said, the OP's OS behavior seem to indicate a complete load of the script which explain the current behavior, albeit does not guarantee it.

Tarik
  • 10,810
  • 2
  • 26
  • 40
  • Thank you. It makes sense. Does it mean that bash script loads a whole file in memory? Is this recent update on bash? This behavior is different from the original post (Edit shell script while it's running) where the script is affected by editing while it's running. – jef May 09 '20 at 02:06
  • @jef The shell executable loads the bash script in memory at once and interprets the commands inside the script. – Tarik May 09 '20 at 10:33
  • 1
    Different versions of bash do different things. Some read the file byte-by-byte as they execute it, some I think load it in 8KB blocks (not necessarily the whole file), some do even more complicated things (AIUI it can also depend on the OS they're running under). See my answer [here](https://stackoverflow.com/questions/21096478/overwrite-executing-bash-script-files#21100710). Net result: do not count on any particular behavior. – Gordon Davisson May 09 '20 at 16:18
  • Thank you, Tarik and Gordon Davisson. I should not change shellscript like python :( – jef May 10 '20 at 06:12