Le Mon, Mar 09, 2026 at 10:26:56AM -0400, Chet Ramey a écrit :
> It's not a `pitfall', simply an invalid assumption.

It's a pitfall for who made this invalid assumption!

Le Mon, Mar 09, 2026 at 08:02:16PM -0400, Zachary Santer a écrit :
> You can literally just enclose your existing bash script between a
> pair of curly braces to force bash to parse the whole thing before it
> starts to run it.
Almost yes:

Quickly with my previous sample:

    #!/bin/bash
    
    main() {
    echo "This is a test script"
    
    script="$(sed \$s/last/second-to-last/ <$0)"
    echo "$script" >$0
    
    echo 'echo This line was added while running' >>$0
    
    echo This is the last line.
    }
    
    main "$@"

Running this will shown:

    This is a test script
    This is the last line.
    This line was added while running

Where de ``second-to-last'' was NOT modified, but a last line
WAS added AND executed before the end of script.

Another *workaround* could consist to use a temporary file to copy original
script before executing temporary file, then drop it at end...

Anyway, as I use bash for a lot of different things, and often as a filter
or through pipes (fifos, ssh, other), I know, understand and accept this
behaviour. ( Not a pitfall anymore! :-) And I'm not a C developper, but
my feeling is: trying to change anything about this seem not as simple
and could lead to many new and unexpected limitations. My layman's opinion
is: don't touch this!


-- 
 Félix Hauri  -  <[email protected]>  -  http://www.f-hauri.ch

Reply via email to