The difference between "exec" and "exit" in bash

I saw both "exit" and "exec" used in a bash script to stop the script from executing if an error occurred. For instance:

if [ ! -f file ]; then echo "no file" exit 1 fi 

and

 if [ ! -f file ]; then exec echo "no file" fi 

What is the best practice here and why? Broader discussions / explanations regarding "exec" and "exit" are also welcome :)

+4
source share
3 answers

exit simply exits the shell, returning the specified numeric exit code (or 0 if omitted) to the parent process. This is equivalent to the C library routine, also called exit , or to return from main .

exec replaces the shell with a new executable image (still running in the same process), which will eventually come out and return some code or another parent process. This is roughly equivalent to the regular C execvp library.

Your first example is an almost, but not quite, correct way to stop a script if an error occurs. He must read

 if [ ! -f file ]; then echo "no file" >&2 exit 1 fi 

>&2 , which is not in your example, causes an error message in stderr where it belongs, instead of stdout where it does not belong.

The second example is incorrect. In addition to reporting the error in the wrong place, exec echo will stop the script (because /bin/echo will do its job and then exit), but it will return exit code 0 to the parent process. Exit code 0 means success. Programs running on a Unix environment must always return a non-zero exit code if they fail.

The correct use of exec is done in shell scripts that do some configuration work and then call a long-lived program written in another language, and after that they have nothing to do, so it makes no sense to keep the shell process hanging around. For instance:

 #! /bin/sh PATH=/special/directory/for/foo:$PATH export PATH exec foo 
+13
source

The exit terminates the current shell with the specified exit code. The exec command replaces the current shell with a new process defined by the arguments. It also effectively terminates the script after the process terminates, with the exit code being the exit code of the new process.

The first code fragment invokes the echo shell internal command, and then terminates the shell with exit code 1. The second replaces the shell with the external echo program, which is likely to exit with exit code 0.

I would definitely recommend the first option. It saves the launch of the external command /bin/echo and correctly indicates an error with the exit code.

+3
source

The correct way to indicate an error is for the script to return a non-zero code, for example 1. In your case, it is best to use "exit 1".

Using "exec" will simply execute the code after it. Perhaps the examples you saw use "#! / Bin / bash -e" or "set -e" and then use "exec" to call the code that fails, resulting in "exec" itself returning non- null code and "-e" means that if any command returns a non-zero code or is false, exit the script immediately.

0
source

Source: https://habr.com/ru/post/1391174/


All Articles