2

I sometimes include this function in my scripts:

function abort {
    echo $@; exit 1
}

It's nice to have a print-and-exit notation contained in a single command. However, when sourcing scripts, exit kills the shell and return does nothing from within the function.

How can I redefine abort() to work in sourced scripts?

salezica
  • 74,081
  • 25
  • 105
  • 166
  • possible duplicate of [How do you return to a sourced bash script?](http://stackoverflow.com/questions/3666846/how-do-you-return-to-a-sourced-bash-script) – Barmar Jun 04 '14 at 21:32
  • Also see http://stackoverflow.com/questions/23749529/how-to-stop-execution-in-script-which-may-have-been-sourced-or-directly-executed – Barmar Jun 04 '14 at 21:32
  • 2
    The short of it: methinks this can't be done in a generic function, UNLESS you place _all your code_ in a _subshell_ and use `exit` there: see http://stackoverflow.com/a/23751042/45375 – mklement0 Jun 04 '14 at 21:32
  • @Barmar Not a duplicate of that particular question, since he's asking about it in a function which handles both sourcing and execution and mentions that `return does nothing from within the function`, or more precisely doesn't do what's intended here. – Reinstate Monica Please Jun 04 '14 at 21:40

0 Answers0