11

Here I don't understand what the following piece of code is trying to achieve, I did search online for the use of exec but don't quite get the idea, could anyone please help to explain?

Code snippet:

exec $(dirname "$0")/init.sh -l interface.mod -l instrument.mod -a postinit.mod -a async.mod "$@"
5
  • 4
    exec allows you to execute a command that completely replaces the current process instead of running the command as a child process. Commented Oct 29, 2020 at 3:21
  • 1
    So what is the good of it? After the command became the main process, does it mean now the main process will take in any commands instead of /bin/bash? E.g, echo 1, then 1 will be passed to the main process. Is this the right understanding? Thanks. Commented Oct 29, 2020 at 3:27
  • 2
    If you wanted to replace your bash shell with zsh, then you could exec /bin/zsh. So you have the idea. If you want to do the same thing, but allow yourself to exit from zsh and be back in bash you could replace within a subshell, e.g. (exec /bin/zsh) Commented Oct 29, 2020 at 3:30
  • Right, I guess I get the basic idea. Commented Oct 29, 2020 at 3:41
  • Mind, that's not the only thing exec does. If instead of giving it arguments you redirect its file descriptors, it applies those redirections to the current process; so exec 2>stderr.log makes everything the shell and programs it starts writes to stderr, instead of going to the TTY or wherever they would have otherwise defaulted to, end up in stderr.log on disk. Commented Jan 9 at 1:52

1 Answer 1

13

The exec is a builtin command of the Bash shell which allows you to execute a command that completely replaces the current process, i.e., the current shell process is destroyed, and entirely replaced by the command you specify. It is useful when you want to run a command, but you don't want a bash shell to be the parent process. When you exec a command, it replaces bash entirely - no new process is forked, no new PID is created, and all memory controlled by bash is destroyed and overwritten. This can be useful if, for instance, you want to give a user restricted access to a certain command. If the command exits because of an error, the user will not be returned to the privileged shell that executed it. exec may also be used without any command, to redirect all output of the current shell to a file. Here is the definition from man bash:

exec [-cl] [-a name] [command [arguments]] If command is specified, it replaces the shell. No new process is created. The arguments become the arguments to command. If the -l option is supplied, the shell places a dash at the beginning of the zeroth argument passed to command. This is what login(1) does. The -c option causes command to be executed with an empty environment. If -a is supplied, the shell passes name as the zeroth argument to the executed command. If command cannot be executed for some reason, a non-interactive shell exits, unless the exec fail shell option is enabled. In that case, it returns failure. An interactive shell returns failure if the file cannot be executed. If command is not specified, any redirections take effect in the current shell, and the return status is 0. If there is a redirection error, the return status is 1.

Sign up to request clarification or add additional context in comments.

1 Comment

exec is also often used as an optimization. If a shell script is going to run some other program as the last thing it does, running the other program without exec means the shell has to sit there waiting for the other process to finish, doing nothing useful. Using exec means the shell hands directly off to the other program, and there's no extra useless process hanging out taking up resources. This seems like it might be the reason it's used here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.