0

I have a script that I want to fail on any errors for one section of the code, but continue through errors in another.

I know 'set -e' will achieve the first requirement, but is there anyway to 'unset -e' for a script, or ignore errors for a specific line or function?

  • 4
    just `set +e` before that line and then `set -e` again – phuclv Mar 08 '23 at 14:34
  • 3
    `set -e` is somewhat controversial => https://mywiki.wooledge.org/BashFAQ/105 – glenn jackman Mar 08 '23 at 14:39
  • Pedantic note: you aren't toggling the setting; you want to explicitly disable it, then explicitly enable it. (Toggling *always* changes the setting; `set -e` doesn't change anything if `errexit` is already enabled. You can toggle an unknown setting and know that whatever the setting *was*, the opposite is now true, even if you still don't know what the new setting is.) – chepner Mar 08 '23 at 14:49
  • See [Bash ignoring error for a particular command](https://stackoverflow.com/q/11231937/4154375). – pjh Mar 08 '23 at 18:12

1 Answers1

7

set -e to enable and set +e to disable.

But if you only want to ignore errors for a single line, consume the exit code of the command on that line:

! failing_command

or

failing_command || :
knittl
  • 246,190
  • 53
  • 318
  • 364