How does the exit command work on a Unix terminal?

5,462

Solution 1

Well usually you would only see execution upon exiting a shell if you've manually configured this. But maybe one of the packages you've installed came with a bash exit shell script...

check;

~/.bash_logout

maybe you'll find a script call from there, it's an odd one...

Solution 2

man bash

  exit [n]
         [...]  A trap on EXIT is executed before the shell terminates.

Such traps are often used to clean up tmpfiles on exit, see https://stackoverflow.com/questions/687014/removing-created-temp-files-in-unexpected-bash-exit

Define an exit trap like this (for better testing in a new shell):

$ bash
$ trap "rm filetodelete" EXIT

Show defined EXIT trap:

$ trap -p EXIT
trap -- 'rm filetodelete' EXIT

Test:

$ exit
rm: cannot remove ‘filetodelete’: No such file or directory

Note that exit may be "called" implicitly too. So instead of exit you could have also triggered the trap by kill -HUP $$.

Solution 3

The exit command is a special built-in command in shells. It has to be built-in as it needs to exit the shell process.

It exits the shell with the exit status provided if any or that of the last command otherwise.

Upon exiting, the shell will run the EXIT traps if any. See the output of trap (in Bourne-like shells) for the currently set ones.

With many shells, if the shell was invoked as a login shell (some systems/users configure terminal emulators to start a login shell), it will also run the code stored in special files like ~/.logout, ~/.zlogout, ~/.bash_logout and possibly corresponding ones in /etc depending on the shell.

You could do a set -x before calling exit to get an idea of where those commands are being run from.

Solution 4

exit is a "builtin" command of bash, so no wonder man exit doesn"t help.

Proper documentation can be obtained from the manual pages man bash, or with the builtin command help of bash (help exit).

$ help exit
exit: exit [n]
    Exit the shell.

    Exits the shell with a status of N.  If N is omitted, the exit status
    is that of the last command executed.
$

If you really want to know how it works, have a look at the source : http://git.savannah.gnu.org/cgit/bash.git/tree/builtins/exit.def?h=bash-4.4

Share:
5,462

Related videos on Youtube

arie64
Author by

arie64

Updated on September 18, 2022

Comments

  • arie64
    arie64 almost 2 years

    Could someone please explain how the exit command works in Unix terminal?

    A search of man exit and which exit was not helpful and I have come across the following issue.

    After installing add on packages for Anaconda and PyCharm on my new Red Hat system I noticed that when ever I called exit to exit out of a terminal session I would get a series of errors, and then the terminal quits as expected. The errors seem to suggest that my call to exit is triggering a call rm ~/anaconda3/.../ and rm ~/PyCharm/...., causing an error. All of the directories also appear to be the locations of packages I downloaded for these programs (i.e. numpy), see below.

    $ exit
    rm: cannot remove ‘~/anaconda3/lib/python3.5/site-packages/numpy/core’: Is a directory
    ...
    ...
    

    Resolved

    In my ~/.bash_logout file, there was a line

    find ~ -xdev ( -name *~ -o -name .*~ -o -name core ) -exec \rm '{}' \;

    Commenting this line out stopped the error messages. It appears to search and delete all temporary files. But it also attempts to find directories with the word "core" in them, and delete those as well. This was a preset in the system.

    • G-Man Says 'Reinstate Monica'
      G-Man Says 'Reinstate Monica' over 7 years
      Just to clarify: are you saying that, after you install add on packages for Anaconda and PyCharm, you get a series of errors when you use exit to exit out of that terminal session — or that that happens every time you use exit after that?
    • arie64
      arie64 over 7 years
      It has occurred for every terminal session after installing these add on packages.
    • Dominique
      Dominique over 7 years
      Very strange, did you already verify if exit is an alias?
    • nneonneo
      nneonneo over 7 years
      The reason why it tries to delete core files is because they are usually crashdump files (core dumps) which take up space but are rarely useful unless you are developing the crashing software. In this case it's attempting to delete a directory named core, which fails (and good thing it fails - numpy.core is essential to NumPy!).
    • Peter Cordes
      Peter Cordes over 7 years
      Most systems these days are configured not to write core dumps by default (by making ulimit -c 0 the default). That find -exec rm command isn't something I'd want anyway, but you could add -type f to it. You can also make it a lot more efficient deleting multiple files by using -exec rm {} + instead of ... \;, so it batches multiple args onto one rm command line (like xargs)
  • countermode
    countermode over 7 years
    The OP was not talking about logout but about exiting a shell.
  • ilkkachu
    ilkkachu over 7 years
    @countermode, they also didn't state that it wasn't a login shell that was running in their terminal session.
  • Angel Todorov
    Angel Todorov over 7 years
    Great advice to debug with set -x
  • arie64
    arie64 over 7 years
    I am in a login shell and this behavior also occurs with a call to <code>logout</code>. In my <code>~/.bash_logout</code> I have a line
  • arie64
    arie64 over 7 years
    <code> find -xdev /( -name *~ -o -name ./*~ -o -name core ) -exec \rm '{}' /;</code> which, after commenting out, the error does not occur. So I think the comments where correct, this line is trying to delete temporary files. Thank you!
  • Peter Cordes
    Peter Cordes over 7 years
    Worth pointing out type -a exit -> exit is a shell builtin is how you know to read the bash man page (or run help exit instead of man) in the first place.
  • Peter Cordes
    Peter Cordes over 7 years
    You can get a log of the set -x output by either running bash in a way that won't clear or close the terminal after it exits, or maybe by doing set -x, exec &> exit_log.txt then blindly type exit.
  • Kevin Fegan
    Kevin Fegan over 7 years
    @arie64 - "<code>" doesn't work in comments. To indicate code within comments on SE, use the "`" (Grave-accent / Left-single-quote) before and after the code. Like: `find -xdev ...` to get find -xdev ....
  • arie64
    arie64 over 7 years
    Ah thank you! I was trying to figure out how to change that.