0

For convenience, I sometimes keep all execution commands within a single bash file and run them according to my applications. Let's say I have a bash script exec.sh as shown below.

python a.py
#python b.py

I notice that sometimes when I run exec.sh to execute python a.py and modify the script before the previous execution finishes:

#python a.py
python b.py

the first execution will continue to run python b.py, even after finishing python a.py.

My question is: why does it continue?

I suppose the first execution only see this. It seems unreasonable to me that the program will runs a comment.

python a.py
#python b.py

My suspection is that the bash interpreter works line-by-line, so at the begining it only sees python a.py, but when it finishes, it will read again the file and see python b.py being available.

The operating system is Ubuntu 22.04.1 LTS with a bash version 5.1.16(1)-release (x86_64-pc-linux-gnu).

I will appreciate any thoughts or any possible reasons that cause this situation.

thanks.

hpwww
  • 535
  • 3
  • 8
  • 3
    The short answer is that it isn't safe to modify a running script; it can cause even weirder problems than this. Exactly how bash handles the file seems to depend on the bash and/or OS version. See my answer to ["Overwrite executing bash script files"](https://stackoverflow.com/questions/21096478/overwrite-executing-bash-script-files). – Gordon Davisson Mar 22 '23 at 09:20
  • @GordonDavisson Thanks a lot! That exactly answeres my question and even provides a solution. – hpwww Mar 22 '23 at 11:55
  • I suggest to close this question with Gordon's link as duplicate. – Cyrus Mar 22 '23 at 14:48
  • @Cyrus Thanks for the suggestion, but I think it not straight-forward to everyone, at least me, to ask the right question at the first place. I would prefer leaving this question here for someone who will have a similar thought/observation. – hpwww Mar 22 '23 at 16:12
  • @hpwww Actually, that's why we mark questions as duplicates -- the idea is that it's better to have all the answers to essentially similar questions in just one place, and have the related questions all point readers/searchers to that collection. See ["Dr. Strangedupe: Or, How I Learned to Stop Worrying And Love Duplication"](https://stackoverflow.blog/2010/11/16/dr-strangedupe-or-how-i-learned-to-stop-worrying-and-love-duplication/). – Gordon Davisson Mar 22 '23 at 17:56

0 Answers0