Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

migrated from askubuntu.com 2 days ago

This question came from our site for Ubuntu users and developers.

    
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? – G-Man 2 days ago
1  
It has occurred for every terminal session after installing these add on packages. – arie64 yesterday
1  
Very strange, did you already verify if exit is an alias? – Dominique yesterday
3  
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!). – nneonneo yesterday
    
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) – Peter Cordes 10 hours ago
up vote 20 down vote accepted

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...

share|improve this answer
    
The OP was not talking about logout but about exiting a shell. – countermode yesterday
1  
@countermode, they also didn't state that it wasn't a login shell that was running in their terminal session. – ilkkachu yesterday
    
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 yesterday
    
<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! – arie64 yesterday
3  
@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 .... – Kevin Fegan yesterday

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 http://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 $$.

share|improve this answer
    
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 yesterday

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.

share|improve this answer
    
Great advice to debug with set -x – glenn jackman yesterday
    
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. – Peter Cordes yesterday

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|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.