Exit the bash function, not the terminal

91

Solution 1

TL;DR

Use return instead of exit AND run your script with source your-script.sh aka. . your-script.sh

Full details

If launching a script with an exit statement in it, you have to launch it as a child of you current child.

If you launch it inside the current shell of started with your terminal session (using . ./<scriptname> any exit will close the main shell, the one started along your terminal session.

If you had launched your script like bash ./<scriptname> (or any other shell instead of bash), then exit would have stopped your child shell and not the one used by your terminal.

If your script has executable permissions, executing it directly without giving the name of the shell will execute it in a child shell too.

Using return instead of exit will allow you to still launch your script using . ./<script name> without closing the current shell. But you need to use return to exit from a function only or a sourced script (script ran using the . ./<scriptname> syntax).

Solution 2

Instead of using exit 1 try using return 1.

Solution 3

The only way that the given script is able to terminate the shell session (and therefore the terminal) is by you sourcing the script (to install the installZook function in the current shell session), and then running the function in the shell.

If what you are showing is only a portion of a larger script containing a call to the installZook function, then the function can still only cause the terminal to terminate if the script is sourced, but not through being run as an ordinary shell script.

exit terminates the current shell session. When the function executes exit it terminates the shell that called it.

return returns from a function (or a sourced script file). If the function, instead of exit, used return, it would return control to the calling environment (probably the interactive shell that you called the function from) without exiting it.

If you manually run the installZook function from the shell, then all you should need to do is to change the exit to return. If there's another piece of code in your script that calls the function (and which you are not showing), then that piece of code needs to additionally react to the return status of the function.

For example

installZook || return

If this was part of no function in the script, and if you sourced the script, it would return control to the shell if the function returned a non-zero exit code.

Share:
91

Related videos on Youtube

MountainMan
Author by

MountainMan

Updated on September 18, 2022

Comments

  • MountainMan
    MountainMan almost 2 years

    Using plain, native Javascript (no jQuery or other framework answers please), how does the response part of an AJAX call get processed and formatted before being sent back to the requesting script?

    I have been looking for this answer and cannot seem to find it in the many searches I have performed. I would like to see an example, and not one focusing on the POST or GET variable access. I want to know how to respond to the script.

    • Dr. Dan
      Dr. Dan over 11 years
      Your question makes no sense to me
    • adeneo
      adeneo over 11 years
      Whatever you send from the client is wrapped up in a POST or GET request, and to get something back from the server all you really have to do is echo or return something in the PHP script, and it's returned to the XMLHttpRequests, if you have the right methods in place to catch it of course (simplified a lot).
    • NullPoiиteя
      NullPoiиteя over 11 years
      jQuery is a multi-browser JavaScript library designed to simplify the client-side scripting of HTML
    • Jared Farrish
      Jared Farrish over 11 years
      Read this question/answer: stackoverflow.com/questions/5485495/… Short answer: Callbacks can accept JSON, XML, HTML (umm, sorta), plaintext. Nowadays you see a lot of people using JSON, because it's lightweight and you can use PHP's echo json_encode($arr); for example. XML and HTML in roll-your-own scripts (NOT jQuery...) can be trickier to get working (or at least back in the MSXML2.XMLHttpRequest it was).
    • MountainMan
      MountainMan over 11 years
      @Dr.Dan Well Then! How about Jared Farrish's question?
    • Jared Farrish
      Jared Farrish over 11 years
      Here's a fairly in-depth article including different examples of responding to an Ajax request with PHP: ajaxpatterns.org/XMLHttpRequest_Call
    • MountainMan
      MountainMan over 11 years
      @Jared There is a difference between editing something and a wholesale rewriting. I am annoyed at that wholesale butchering rewriting. A Question YOU wrote and which makes sense to you is all fine, I see you are quite experienced or at least have build up a lot of rep, but please, what is the point of such a wholesale replacement of one question with another?
    • argentage
      argentage over 11 years
      There is no need to be rude. If you see a difference between what he edited and your question's intent, edit it- but in any case it wasn't possible to tell what was being asked originally.
    • Jared Farrish
      Jared Farrish over 11 years
      Revert it. The content on this site in the end belongs to the community, so there is a sense that what is in the questions and at times answers needs to be focused and clarified to make it an effective question or answer. There was a lot of noise and frankly, you were likely to get more hassle than help because the question was noisy and easily misread. But, you can revert it by clicking the link to the time above my name if it offends you that much or I got it that wrong. You won't hurt my feelings either way.
    • MountainMan
      MountainMan over 11 years
      @airza Hmm.. Well, I could have I admit made the case instead of expressing frustration, that a question, with many flaws to an experienced coder, nevertheless might give many folk an idea of the areas the OP lacks understanding of, and tooheavy editing sorta defeats that. In any case, the link he provided looks like EXACTLY WHAT I HAD HOPED TO FINDF for a study resource.
    • Jared Farrish
      Jared Farrish over 11 years
      (And although your english is good enough, we get a lot of users who struggle. We "wholesale rewrite" usually to make the question accessible, and to me this was no different.)
    • MountainMan
      MountainMan over 11 years
      @Jared. Gotcha. No, no revert... and thanks. I go study now.
    • MountainMan
      MountainMan over 11 years
      OMG it is better than most textbooks. I'll be a couple weeks soaking that up and practicing.
    • Admin
      Admin about 10 years
      How do you launch this script ?
    • Admin
      Admin about 10 years
      use return 1 instead of exit 1
    • Admin
      Admin over 2 years
      Didn't have the time to check fully out, but builtin caller might give away how much exiting exit will trigger...
  • MountainMan
    MountainMan over 11 years
    ..So in the broadest sense, it is like the php script is POSTING, but not to another script per se, the <script type="text/Javascript" but to PARTICULARLY the AJAX request, and not via a POST, but through a header? Thanks, way ahead of a hundred Google resuts, right there.
  • argentage
    argentage over 11 years
    Yes. It isn't accurate to say that it's replying through a POST to the script, but that part of an AJAX request implies a return which is specified in PHP by setting the headers and echoing the response.
  • Barmar
    Barmar about 10 years
    return will return from the InstallZook() function, not exit the script.
  • Kusalananda
    Kusalananda over 5 years
    @NamGVU Returning rather than exiting is the correct thing to do. Assuming that they source the file and then call the function (the exit/return in the function would not be called while sourcing the given file), the shell session would terminate (together with the terminal) if the function used exit. With return, it would handle control back to the calling shell. Note that if you want to return from a sourced script (which this is not about), return is the correct way to do that too as exit would terminate the shell session.
  • Kusalananda
    Kusalananda over 5 years
    @Barmar See comment above.
  • clay
    clay over 5 years
    It depends on how you run the script at first place
  • Kusalananda
    Kusalananda over 5 years
    @NamGVU There is no call to the function in the file. Just sourcing or running the file would not call the function. Explain how the function terminates the shell session and terminal in any other way than by first sourcing the file and then calling it in the terminal.
  • clay
    clay over 5 years
    Thanjs for sharing. I think we not going too far from the topic then.