I have created a shell function to comfortably use desktop search tool baloosearch. I created it as a function for a reason.

Now, in this functions I need to use exit command to quit loops, ifs or case conditions. The problem is, when the exit commands gets executed, it also kills my terminal window...

My question is, is there a way to use exit, but without closing the terminal window?

When using this thing as a shell script, it's okay, but I need it to be run as a BASH function...

  • 1
    It's hard to suggest alternatives without seeing the code in question - but typically you would break from loops and return from functions – steeldriver Jun 13 at 21:42
  • I just found it, return exits functions, without closing the shell. I created a wrapper script/function to use baloosearch. When doing a search this function allows to change directory into the found object. To achieve this, it needs to be a function, not a script. I might publish it somewhere some time later. It may be useful for people who like baloosearch. – flyingpenguin Jun 13 at 21:48

After 1.5 minutes of research I can now answer myself.

The solution is to use return instead of exit. This way exitting the function does not close the terminal, it leaves the shell session open.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.