What kinds of commands can run with exec? For example, executable files, builtin commands, shell script, compound commands (e.g. looping, conditional branching), pipeline, command list, commands grouped by parentheses or braces, ...?

Because the parent shell exits upon the command run with exec finishes running, it is hard to tell if there is an error.

  • 1
    I suspect it's just executable files, including scripts. exec replaces the current process with the first argument, so that must be something that could be a process I suspect. Also, the parent shell does not actually exit when exec finishes, the parent shell ceases to exist when exec starts Apr 16 '16 at 14:41
  • By cease to exit, do you mean the parent process hasn't exit when exec starts? When will the parent shell exit?
    – Tim
    Apr 16 '16 at 16:36
  • 1
    No, the parent process is replaced by the target of the exec. It doesn't exit. It is replaced.
    – roaima
    Apr 16 '16 at 18:14
  • @roaima: when the command run with exec finishes running, does the parent/shell process exit?
    – Tim
    Apr 22 '16 at 17:37
  • « No, the parent process is replaced by the target of the exec. It doesn't exit because there is no parent process left. It has been replaced. » I don't know how to explain it more succinctly than this.
    – roaima
    Apr 22 '16 at 19:22

exec replaces the current shell executable by another executable, i.e. an external command, in the same process. Since there is no shell to execute anything, shell concepts such as aliases, functions, builtins, or compound commands cannot be invoked via exec. (The syntax doesn't allow compound commands anyway.)

The shell does not exit when the command run with exec finishes running. The shell program terminates when it runs exec, in the sense that its code stops running (but, once again, the process does not exit). The shell process keeps running the new program. When the executed program terminates, the parent of the process is notified as usual; it is up to it to react to any error.

  • Thanks. What is "the parent of the process"?
    – Tim
    Apr 17 '16 at 3:02

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.