Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I've continuously observed the described problem, and have read about a workaround: when writing the "top level" lines of script, never write them alone, but inside of a { }

It looks like this in my case:

    somefunction() 
    { 
    }

    # main
    {
       ...
    }
Now whatever is inside of any of the functions or inside of the "main" braces will be read at once and it won't be replaced with some new lines if I edit the file while the execution still hasn't finished.

I would surely prefer having some global flag "reread the script" for those who need the old behavior and "sane" default for the most users (never reread). But general user friendliness and reasonable defaults was seldom a desired goal in the circles that decide about the development of these programs.




Hm I put all shell code in functions for other reasons, but it's interesting to know it solves another problem.

http://www.oilshell.org/blog/2020/02/good-parts-sketch.html#...

To summarize:

1) saving many interactive snippets in a single file -- each one is a function

2) expose entire shell functions to tools like xargs and chroot

3) if $0 myfunc solves the "ignored errexit" problem

now

4) safely modify a bash script while it's executing




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: