LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Exit vs Return (https://www.linuxquestions.org/questions/linux-newbie-8/exit-vs-return-657761/)

JimRobinson 07-23-2008 02:51 PM

Exit vs Return
 
I can't find out why exit is preferred over return to terminate a batch process. I can see where the trap processing can be useful using return but I think exit utilizes it as well.

Am I safe to continue using return instead of exit?

Mr. C. 07-23-2008 06:18 PM

From the main code line, they are the same. There is an implicit exit after returning from the main code line.

Use return in functions. Use exit to explicitly exit from anywhere with a status code.

JimRobinson 07-24-2008 06:51 AM

Quote:

Originally Posted by Mr. C. (Post 3224249)
From the main code line, they are the same. There is an implicit exit after returning from the main code line.

Use return in functions. Use exit to explicitly exit from anywhere with a status code.

Thanks Mr. C. I appreciate the input.
I typically use "return 1" to abend my batch processes and "return 0" to complete the process. return and exit will perform the same in my case right? I ask because out of habit I have changed alot of the existing code from exit to return 1.

Mr. C. 07-24-2008 12:26 PM

So perhaps we need to clarify what you mean by "batch process".


Consider the following script and output:

Code:

$ cat hello_world
#!/bin/bash

echo Hello World
return 0

$./hello_world
Hello World
./hello_world: line 4: return: can only `return' from a function or sourced script
$ echo $?
1

but if we change the interpreter to #!/bin/sh
Code:

$ cat hello_world
#!/bin/sh

echo Hello World
return 0
$ ./hello_world
Hello World
$ echo $?
0

We see that sh allows return to be the same as exit at the end of the main code line. Now let's run it in the way I think you mean by "batch":

Code:

$ bash ./hello_world
Hello World
./hello_world: line 4: return: can only `return' from a function or sourced script
$ echo $?
1
$ sh ./hello_world
Hello World
$ echo $?
0

Same thing. The final form of executing the shell scripting code is via the . (dot) source command:

Code:

$ . ./hello_world
Hello World
 $ echo $?
0

Now, change the return to an exit, and try each of the above tests. I'll leave that to you to test out. But this will be the thing that makes your choice clear, so be sure to actually perform the three execution variants. And then the man page segment shows:
.

Code:

      return [n]
              Causes  a function to exit with the return value specified by n.
              If n is omitted, the return status is that of the  last  command
              executed  in the function body.  If used outside a function, but
              during execution of a script by  the  .  (source)  command,  it
              causes the shell to stop executing that script and return either
              n or the exit status of the last  command  executed  within  the
              script  as  the  exit  status  of the script.  If used outside a
              function and not during execution of a script by .,  the  return
              status is false.  Any command associated with the RETURN trap is
              executed before execution resumes after the function or  script.



All times are GMT -5. The time now is 06:03 AM.