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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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 The shell executable loads the bash script in memory at once and interprets the commands inside the script.
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. Net result: do not count on any particular behavior.
Thank you, Tarik and Gordon Davisson. I should not change shellscript like python :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.