On Sat, Sep 28, 2024 at 15:17:47 +0100, Alain D D Williams wrote: > On Sat, Sep 28, 2024 at 10:13:59AM -0400, Greg Wooledge wrote: > > On Sat, Sep 28, 2024 at 14:53:10 +0100, Tim Woodall wrote: > > > Is there a way in bash to guarantee that a trap gets called for cleanup > > > in a script? > > > > #!/bin/bash > > trap cleanup EXIT > > cleanup() { > > ... > > } > > > > This works in bash -- i.e., it calls the cleanup function regardless > > of whether the shell exits by calling "exit", or by falling off the > > end of the script, or by receiving a fatal signal. It does NOT work in > > /bin/sh (dash, or any other implementation). You have been warned. > > *ALL* scripts MUST start with a #! line as you have given above. > > Anyone who does not do that deserves what they get.
Yes, but many, MANY people don't understand the difference between #!/bin/bash and #!/bin/sh. Tim specifically asked for an answer that works "in bash", and that's what I gave. But someone else reading this thread in the archives might try to use this with a /bin/sh script -- and that won't work. That's why I'm giving the warning. Even if Tim doesn't need it, there are thousands of other people who will.