8

I know that this is probably a relatively pointless question, but I am curious as to why exec false closes the terminal just like exit does.

I would also like to know if this is a acceptable way to close the terminal or not.

10

exec false is replacing the current shell by the execution of the command false (here not the shell builtin but /bin/false or whatever false executable that comes first in the PATH) which quickly exits. If the shell was the topmost process running in your terminal emulator, there are no more processes running inside it so the terminal emulator is closed.

This is a an acceptable alternate way to close a terminal, just like would be many similar commands:

exec true
exec sleep 0
exec echo
...

See also: what-does-an-exec-command-do

  • As a side note, bash will perform certain cleanup before invoking exec including writing commands to .bash_history. So if one simply wants to save to .bash_history and launch a new shell in the same terminal window, one can do so by typing exec bash. – kasperd Mar 12 '15 at 7:53
  • Ok thanks you answered my question perfectly :) – dragonloverlord Mar 12 '15 at 12:36
  • And a good question! therefore upvoted both! ;) – Fabby Mar 14 '15 at 15:53
  • @jiliagre exec calls commands found in PATH , not built-ins. For instance, exec [[ $USER = root ]] will return bash: exec: [[: not found error. In OP's case, false that is called is /bin/false, and not the shell built-in. Otherwise, good answer, hence +1. As a side note, exec can be sort of a cut-off command, i.e. if those three commands in your example were made into a script, nothing beyond first exec would be reached, because exec would replace shell called by script with whatever command is on the right of exec. – Sergiy Kolodyazhnyy Jul 2 '18 at 6:31

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.