shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

SC2093 - Add 'trap' to the list of commands after 'exec' that it doesn't trigger on

Open Gregory-N-able opened this issue 2 months ago • 1 comments

For bugs with existing features

  • Rule Id (if any, e.g. SC2093):
  • My shellcheck version (shellcheck --version or "online"): 0.11.0 and online.
  • [x] The rule's wiki page does not already cover this (e.g. https://shellcheck.net/wiki/SC2086)
  • [x] I tried on https://www.shellcheck.net/ and verified that this is still a problem on the latest commit

Here's a snippet or screenshot that shows the problem:

#!/usr/bin/env bash
trap 'echo "fail"; exit 1' ERR EXIT
printf "\\n***  Test. ***\\n"
exec mybash
trap - SIGINT SIGHUP SIGTERM
exit

Here's what shellcheck currently says:

$ shellcheck myscript
 
[Line 4:](javascript:setPosition(4, 1))
exec mybash
^-- [SC2093](https://www.shellcheck.net/wiki/SC2093) (warning): Remove "exec " if script should continue after this command.

Here's what I wanted or expected to see:

https://github.com/koalaman/shellcheck/wiki/SC2093#exceptions

'trap' should be added to the exceptions. If you are using trap to catch the failure of exec, you need to clean up after yourself.

Gregory-N-able avatar Oct 29 '25 21:10 Gregory-N-able

Bash, and I believe that this behavior is also true for POSIX shell, reads scripts one line of text at a time. Many languages have separate compile-time and runtime phases, where they'll read the entire thing at once prior to execution.

The effect is that, in Bash, the second trap command wouldn't be included in an execution environment until the exit command.

As a result, traps should usually be placed near the beginning of the file.

Cheers, Wiley

On Wed, Oct 29, 2025, 14:11 Gregory Young @.***> wrote:

Gregory-N-able created an issue (koalaman/shellcheck#3345) https://github.com/koalaman/shellcheck/issues/3345 For bugs with existing features

  • Rule Id (if any, e.g. SC2093):
  • My shellcheck version (shellcheck --version or "online"): 0.110.
  • The rule's wiki page does not already cover this (e.g. https://shellcheck.net/wiki/SC2086)
  • I tried on https://www.shellcheck.net/ and verified that this is still a problem on the latest commit

Here's a snippet or screenshot that shows the problem:

#!/usr/bin/env bashtrap 'echo "fail"; exit 1' ERR EXITprintf "\n*** Test. ***\n"exec mybashtrap - SIGINT SIGHUP SIGTERMexit

Here's what shellcheck currently says:

$ shellcheck myscript

[Line 4:](javascript:setPosition(4, 1)) exec mybash ^-- SC2093 (warning): Remove "exec " if script should continue after this command.

Here's what I wanted or expected to see:

https://github.com/koalaman/shellcheck/wiki/SC2093#exceptions

'trap' should be added to the exceptions. If you are using trap to catch the failure of exec, you need to clean up after yourself.

— Reply to this email directly, view it on GitHub https://github.com/koalaman/shellcheck/issues/3345, or unsubscribe https://github.com/notifications/unsubscribe-auth/AUF2F22NFEYP4UP7UIIVOS332EUOZAVCNFSM6AAAAACKTCQTYWVHI2DSMVQWIX3LMV43ASLTON2WKOZTGU3DOOJQG4ZDCNY . You are receiving this because you are subscribed to this thread.Message ID: @.***>

wileyhy avatar Nov 03 '25 10:11 wileyhy